urllib2.urlopen can't look up a hostname using my normal DNS server; dig etc. can
I'm trying to troubleshoot a problem with urllib2.urlopen. It can't seem to make use of my regular DNS server. Below is the result of several attempts to open various URLs, first using Google public DNS (8.8.8.8) and second using my router. gridley is defined in my /etc/hosts file.
Python 2.7.2 (default, Jun 29 2011, 11:17:09)
[GCC 4.6.1] on linux2
>>> import urllib2
>>> urllib2.urlopen("http://gridley")
<addinfourl at 158490988 whose fp = <socket._fileobject object at 0xb745f7ec>>
>>> urllib2.urlopen("http://google.com")
<addinfourl at 158492204 whose fp = <socket._fileobject object at 0x971842c>>
Python 2.7.2 (default, Jun 29 2011, 11:17:09)
[GCC 4.6.1] on linux2
>>> import urllib2
>>> urllib2.urlopen("http://gridley")
<addinfourl at 154808684 whose fp = <socket._fileobject object at 0xb73997ec>>
>>> urllib2.urlopen("http://google.com")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 394, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 412, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1199, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno -5] No address associated with hostname
However, querying google.com with dig shows that it definitely should work:
; <<>> DiG 9.8.1 <<>&开发者_C百科gt; google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44377
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 190 IN A 74.125.225.83
google.com. 190 IN A 74.125.225.81
google.com. 190 IN A 74.125.225.82
google.com. 190 IN A 74.125.225.80
google.com. 190 IN A 74.125.225.84
;; Query time: 23 msec
;; SERVER: 192.168.7.254#53(192.168.7.254)
;; WHEN: Sun Sep 18 19:46:12 2011
;; MSG SIZE rcvd: 108
IPv6 caused some other problems, but it's completely off now, so that can't be it. What the heck is going on?
This isn't exactly an answer...but note that using dig
does not look up hostnames the same way as, say, urlopen
does. dig
is strictly a DNS query tool, whereas your system may be configured to look for host information in multiple source (a local hosts file, NIS, LDAP, etc).
When you type:
urllib2.urlopen("http://google.com")
Your operating may check several sources. On a Linux system, exactly what gets checked is controlled by the hosts
entry in /etc/nsswitch.conf
. You would use the getent
tool to check hostname looks in a manner similar to what urlopen
is doing:
getent hosts google.com
Your questions doesn't mention what OS you're using, nor does it make explicitly exactly what changed between the first and second examples (did you edit a file? Change a setting in a preferences dialog?).
If you're on Linux, including your resolv.conf
before and after any changes as well as your nsswitch.conf
might help diagnose things. If you're not on Linux, specifying your OS may lead to more useful answers.
精彩评论