Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4243248
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:33:11+00:00 2026-05-21T03:33:11+00:00

In one of my scripts I’m using the following infinite loop to check for

  • 0

In one of my scripts I’m using the following infinite loop to check for an active internet connection:

def online_check():
    try:
        con = urllib2.urlopen("http://www.google.com/")
        data = con.read()
        logging.debug('{0} Reached the host. Exiting online_check'.format(time.strftime('[ %H:%M:%S ]')))
    except:
        logging.debug('{0} Could not reach host trying again in 3 seconds'.format(time.strftime('[ %H:%M:%S ]')))
        time.sleep(3)
        online_check()

I am aware that this is not a very elegant solution but the problem is that when I start my script it will sometimes call the online_check method and get stuck in the middle of it (Once in roughly 200 attempts). The script is still running and no exception is thrown; the script is just stuck. I can press CTRL+C (Even after hours of the script being stuck) and it will just throw an exception and continue with the next online_check. I also rewrote the script to check for an IP address in ‘ifconfig’ as opposed to pinging google, unfortunately with similar results.

What am I doing wrong? Can I rewrite the script so this never happens? Is there something I can do to find out what is happening here?

Help is greatly appreciated. Btw. I’m using Python2.7.1 and I’ve tried this script both on Linux and Mac.

P.S: If you have recommendations for designing a method that checks for connectivity without using bandwidth and minimal overhead, I’d be more than happy to hear it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:33:12+00:00Added an answer on May 21, 2026 at 3:33 am

    In addition to the infinite recursion (CPython as far as I’m aware doesn’t support optimized tail-end recursion), you are not closing your connection.

    You might be hitting some kind of connection limit either with the OS or with Google when it retries. This probably happens before reaching max recursion depth which is why you don’t get an exception.

    The connection would normally close when it get garbage collected, but I think because of the recursion the variable doesn’t completely go out of scope until the recursion ends.

    Also as @senderle mentioned, you should try to catch a more specific error.

    Try:

    def online_check():
      while True:
        try:
          con = urllib2.urlopen("http://www.google.com/")
          data = con.read()
          logging.debug('{0} Reached the host. Exiting online_check'.format(time.strftime('[ %H:%M:%S ]')))
        except urllib2.URLError:
          logging.debug('{0} Could not reach host trying again in 3 seconds'.format(time.strftime('[ %H:%M:%S ]')))
          time.sleep(3)
        finally:
          con.close()
    

    (Warning untested code)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using one master page.In that I am registering java scripts and checking
I'm following a bioinformatics text, and this represents one of my first Perl scripts.
I'm using 2 jquery scripts for my Magento store. One of those scripts, a
using selenium 2 with webdriver one of my scripts needs to save a report
I have a large amount of data to move using two PHP scripts: one
I am using CakePHP and am using 2 javascript scripts one of which relies
any one using selenium scripts with jmeter for performance testing
I'm trying to figure out why one of our migration scripts is taking forever
I'm currently re-writing one of my old PHP scripts so that the code is
With my database upgrade scripts, I typically just have one long script that makes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.