Tell urllib2 to use custom DNS

Looks like name resolution is ultimately handled by socket.create_connection. -> urllib2.urlopen -> httplib.HTTPConnection -> socket.create_connection Though once the “Host:” header has been set, you can resolve the host and pass on the IP address through down to the opener. I’d suggest that you subclass httplib.HTTPConnection, and wrap the connect method to modify self.host before passing … Read more

Python check if website exists

You can use HEAD request instead of GET. It will only download the header, but not the content. Then you can check the response status from the headers. For python 2.7.x, you can use httplib: import httplib c = httplib.HTTPConnection(‘www.example.com’) c.request(“HEAD”, ”) if c.getresponse().status == 200: print(‘web site exists’) or urllib2: import urllib2 try: urllib2.urlopen(‘http://www.example.com/some_page’) … Read more