Testing for connection to a website

Larry Bates larry.bates at websafe.com`
Tue Jul 15 22:00:11 CEST 2008


Alexnb wrote:
> Okay, I already made this post, but it kinda got lost. So anyway I need to
> figure out how to test if the user is able to connect to a specific website.
> Last time I got pointed to the urllib2 page, but if I do urlopen() and and
> am not connected, the program stops. So I don't know if that was what you
> guys wanted me to do, but I don't think so, you guys are smarter than that.
> So, how can I test for connection to a website. 

You can test DNS resolution by doing this:

import socket
#
# Check to make sure domain is legal and that I can resolve it to IP addr
#
try:
     ipaddr=socket.gethostbyname(domain)
except socket.gaierror:
     print "Internet connection or DNS error"

For the "connect to a specific website" portion you just have to wait until it 
times out to determine failure.

-Larry



More information about the Python-list mailing list