简体   繁体   English

urlopen在Python 2.7中有效,但在Python 3.4中失败

[英]urlopen works in Python 2.7 but fails in Python 3.4

I have both Python 2.7.10 and Python 3.4.4 running on my computer. 我的计算机上同时运行了Python 2.7.10和Python 3.4.4。 The following code works in Python 2.7.10: 以下代码在Python 2.7.10中有效:

import urllib2
print urllib2.urlopen('http://google.com').read()

However, if I move on to Python 3.4.4 and run 但是,如果我继续使用Python 3.4.4并运行

import urllib.request
print(urllib.request.urlopen('http://google.com').read())

I get a long pair of errors: 我收到一长串错误:

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>

I have come to understand that an error like this represents problems with the windows settings as suggested here (I'm running Windows 7), but what I can't understand is how it works in 2.7 and not in 3.4. 我已经了解到,这样的错误表示此处建议的Windows设置存在问题(我正在运行Windows 7),但是我无法理解的是它在2.7中的工作原理,而不是在3.4中的工作原理。

After working at the solution, the Python 2.7 code began throwing the same error. 在解决方案上工作之后,Python 2.7代码开始引发相同的错误。 Out of frustration I began applying the microscoft fix. 出于沮丧,我开始使用microscoft修复程序。 I started by logging off and logging back in again. 我先注销然后重新登录。 Everything works as intended now... 现在一切都按预期进行...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM