Python – How to find out whether computer is connected to internet

internet-connectionpython

How to find out whether computer is connected to internet in python?

Best Answer

If you have python2.6 you can set a timeout. Otherwise the connection might block for a long time.

try:
    urllib2.urlopen("http://example.com", timeout=2)
except urllib2.URLError:
    # There is no connection