Question regarding DNS resolution in urllib2

Chris Angelico rosuav at gmail.com
Tue Jun 14 20:43:23 EDT 2011


On Wed, Jun 15, 2011 at 4:34 AM, saurabh verma <nitw.saurabh at gmail.com> wrote:
> hi ,
>
> I trying to use urllib2 in my script , but the problem is lets say a domains resolves to multiple IPs , If the URL is served by plain http , I can add “Host: domain” header and check whether all IPs are returning proper responses or not , but in case of https , I have to trust on my local machines dns resolver and I can’t apply host header in the request .

Regarding Host: headers, experimentation showed that urllib2 did
indeed send one (I tested using Python 2.7.1 on Windows, talking to a
snooping HTTPS server running on a Linux box beside me - source code
available if you're curious, but it's not Python).

>>> import urllib2
>>> req=urllib2.Request(url='https://mirlemont/')
>>> f=urllib2.urlopen(req)
>>> f.read()
'Hello, world!'

Meanwhile the snoop server reports:

conn = Protocols.HTTP.Server.Request("GET" "/")
GET / HTTP/1.1
Accept-Encoding: identity
Host: mirlemont
Connection: close
User-Agent: Python-urllib/2.7

(Yes, my computer's name is Mirlemont. What's yours'? :) )

You could control the selection of IP address using a hosts file. In
Unix, that's /etc/hosts; in Windows,
c:\windows\system32\drivers\etc\hosts; in OS/2, c:\mptn\etc\hosts;
etc. The urllib2 resolver should respect that.

Chris Angelico



More information about the Python-list mailing list