我有一个我正在测试的简单网站.它在localhost上运行,我可以在我的Web浏览器中访问它.索引页面只是"运行"一词. urllib.urlopen将成功阅读该页面,但urllib2.urlopen不会.这是一个演示问题的脚本(这是实际的脚本而不是简化不同的测试脚本):
import urllib, urllib2
print urllib.urlopen("http://127.0.0.1").read() # prints "running"
print urllib2.urlopen("http://127.0.0.1").read() # throws an exception
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
Traceback (most recent call last):
File "urltest.py", line 5, in <module>
print urllib2.urlopen("http://127.0.0.1").read()
File "C:\Python25\lib\urllib2.py", line 121, in urlopen
return _opener.open(url, data)
File "C:\Python25\lib\urllib2.py", line 380, in open
response = meth(req, response)
File "C:\Python25\lib\urllib2.py", line 491, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python25\lib\urllib2.py", line 412, in error
result = self._call_chain(*args)
File "C:\Python25\lib\urllib2.py", line 353, in _call_chain
result …Run Code Online (Sandbox Code Playgroud)