urlopen 在 Python 2.7 中有效,但在 Python 3.4 中失败
urlopen works in Python 2.7 but fails in Python 3.4
我的电脑上有 Python 2.7.10 和 Python 3.4.4 运行ning。以下代码适用于 Python 2.7.10:
import urllib2
print urllib2.urlopen('http://google.com').read()
但是,如果我转到 Python 3.4.4 和 运行
import urllib.request
print(urllib.request.urlopen('http://google.com').read())
我得到一对长错误:
Traceback (most recent call last):
File "C:\Python34\lib\urllib\request.py", line 1183, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Python34\lib\http\client.py", line 1137, in request
self._send_request(method, url, body, headers)
File "C:\Python34\lib\http\client.py", line 1182, in _send_request
self.endheaders(body)
File "C:\Python34\lib\http\client.py", line 1133, in endheaders
self._send_output(message_body)
File "C:\Python34\lib\http\client.py", line 963, in _send_output
self.send(msg)
File "C:\Python34\lib\http\client.py", line 898, in send
self.connect()
File "C:\Python34\lib\http\client.py", line 871, in connect
self.timeout, self.source_address)
File "C:\Python34\lib\socket.py", line 516, in create_connection
raise err
File "C:\Python34\lib\socket.py", line 507, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
print(urllib.request.urlopen('http://google.com').read())
File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 464, in open
response = self._open(req, data)
File "C:\Python34\lib\urllib\request.py", line 482, in _open
'_open', req)
File "C:\Python34\lib\urllib\request.py", line 442, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 1211, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Python34\lib\urllib\request.py", line 1185, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
我已经明白,像这样的错误表示 windows 设置的问题 here(我 运行ning Windows 7),但我无法理解的是它在 2.7 而不是 3.4 中是如何工作的。
解决方案后,Python 2.7 代码开始抛出相同的错误。出于沮丧,我开始应用 microscoft 修复程序。我首先注销并重新登录。现在一切正常...
我的电脑上有 Python 2.7.10 和 Python 3.4.4 运行ning。以下代码适用于 Python 2.7.10:
import urllib2
print urllib2.urlopen('http://google.com').read()
但是,如果我转到 Python 3.4.4 和 运行
import urllib.request
print(urllib.request.urlopen('http://google.com').read())
我得到一对长错误:
Traceback (most recent call last):
File "C:\Python34\lib\urllib\request.py", line 1183, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Python34\lib\http\client.py", line 1137, in request
self._send_request(method, url, body, headers)
File "C:\Python34\lib\http\client.py", line 1182, in _send_request
self.endheaders(body)
File "C:\Python34\lib\http\client.py", line 1133, in endheaders
self._send_output(message_body)
File "C:\Python34\lib\http\client.py", line 963, in _send_output
self.send(msg)
File "C:\Python34\lib\http\client.py", line 898, in send
self.connect()
File "C:\Python34\lib\http\client.py", line 871, in connect
self.timeout, self.source_address)
File "C:\Python34\lib\socket.py", line 516, in create_connection
raise err
File "C:\Python34\lib\socket.py", line 507, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
print(urllib.request.urlopen('http://google.com').read())
File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 464, in open
response = self._open(req, data)
File "C:\Python34\lib\urllib\request.py", line 482, in _open
'_open', req)
File "C:\Python34\lib\urllib\request.py", line 442, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 1211, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Python34\lib\urllib\request.py", line 1185, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
我已经明白,像这样的错误表示 windows 设置的问题 here(我 运行ning Windows 7),但我无法理解的是它在 2.7 而不是 3.4 中是如何工作的。
解决方案后,Python 2.7 代码开始抛出相同的错误。出于沮丧,我开始应用 microscoft 修复程序。我首先注销并重新登录。现在一切正常...