Testing for Internet Connection

Grant Edwards grante at visi.com
Tue Jul 15 20:28:51 CEST 2008


On 2008-07-15, Alexnb <alexnbryan at gmail.com> wrote:

> What exactly do you think will work? I am not sure what you
> think I should do? If I use urlopen("http://www.google.com")
> and I am not connected, I am not going to get an exception,
> the program will fail.

Bullshit.  You get an exception.  Here's my program:

   import urllib2
   try:
       con = urllib2.urlopen("http://www.google.com/")
       data = con.read()
       print data
   except:
       print "failed"

If I run it with no internet connection, I get this:

   $ python testit.py
   failed

If I bring up the internet connection, then I get a bunch of
HTML.   

-- 
Grant Edwards                   grante             Yow! I'm ZIPPY the PINHEAD
                                  at               and I'm totally committed
                               visi.com            to the festive mode.



More information about the Python-list mailing list