Just check a google server is one of the IP-addresses for google.com. Change to whatever site can be expected to respond quickly.
Using a numerical IP-address avoids a DNS lookup,
which may block the urllib2.urlopen call for more than a second. Thanks to @rzetterberg for pointing this out.
By specifying the timeout=1 parameter, the call to urlopen will not take much longer than 1 second even if the internet is not “on”.


1. unutbu, http://stackoverflow.com/questions/3764291/checking-network-connection

About Gugulethu Ncube

IT enthusiast getting things done.