urlopen在Python 2.7中工作,但在Python 3.4中失败

urlopen在Python 2.7中工作,但在Python 3.4中失败,python,windows,urlopen,Python,Windows,Urlopen,我的计算机上同时运行着Python 2.7.10和Python 3.4.4。以下代码适用于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()) 我得到一对很长的错误: Traceba

我的计算机上同时运行着Python 2.7.10和Python 3.4.4。以下代码适用于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>
回溯(最近一次呼叫最后一次):
文件“C:\Python34\lib\urllib\request.py”,第1183行,在do\u open中
h、 请求(请求获取方法(),请求选择器,请求数据,标题)
请求中第1137行的文件“C:\Python34\lib\http\client.py”
self.\u发送请求(方法、url、正文、标题)
文件“C:\Python34\lib\http\client.py”,第1182行,在发送请求中
self.endheaders(主体)
文件“C:\Python34\lib\http\client.py”,第1133行,在endheaders中
自发送输出(消息体)
文件“C:\Python34\lib\http\client.py”,第963行,在发送输出中
self.send(msg)
文件“C:\Python34\lib\http\client.py”,第898行,在send中
self.connect()
文件“C:\Python34\lib\http\client.py”,第871行,在connect中
self.timeout,self.source\u地址)
文件“C:\Python34\lib\socket.py”,第516行,位于create\u connection中
提出错误
文件“C:\Python34\lib\socket.py”,第507行,位于create\u connection中
sock.connect(sa)
TimeoutError:[WinError 10060]连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机没有响应
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
打印(urllib.request.urlopen('http://google.com“).read())
文件“C:\Python34\lib\urllib\request.py”,第161行,在urlopen中
返回opener.open(url、数据、超时)
文件“C:\Python34\lib\urllib\request.py”,第464行,打开
响应=自身打开(请求,数据)
文件“C:\Python34\lib\urllib\request.py”,第482行,处于打开状态
"开放",
文件“C:\Python34\lib\urllib\request.py”,第442行,在调用链中
结果=func(*args)
文件“C:\Python34\lib\urllib\request.py”,第1211行,在http\u open中
返回self.do_open(http.client.HTTPConnection,req)
文件“C:\Python34\lib\urllib\request.py”,第1185行,在do\u open中
引发URL错误(err)
urllib.error.urleror:

我逐渐了解到,像这样的错误代表了建议的windows设置问题(我运行的是windows 7),但我不明白的是它在2.7中是如何工作的,而不是在3.4中

在处理解决方案之后,Python 2.7代码开始抛出相同的错误。出于沮丧,我开始应用microscoft修复程序。我从注销和重新登录开始。现在一切都正常了…

我想他们更改了包名…建议:只需使用。在GNU/Linux上使用Python 3.2.3,效果很好。可能是Windows编译的问题……我在本地(在Windows 7;Python 3.5.0上)测试了这个问题,它对我很有效。所以这不是Python代码的问题,而是其他地方的问题。我很想在这个项目中简单地使用2.7,而不是试图追踪问题,但我宁愿修复问题也不愿避免它。。。