2016-06-08 17 views
2

Ich habe sowohl Python 2.7.10 und Python 3.4.4 auf meinem Computer ausgeführt. Der folgende Code funktioniert in Python 2.7.10:Urlopen funktioniert in Python 2.7 aber schlägt in Python 3.4 fehl

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

Allerdings, wenn ich zu Python 3.4.4 bewegen auf und laufen

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

ich ein langes Paar Fehler erhalten:

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> 

Ich habe verstanden, dass ein Fehler wie dieser Probleme mit den Windows-Einstellungen wie vorgeschlagen here darstellt (ich verwende Windows 7), aber was ich nicht verstehen kann ist, wie es in 2.7 und nicht in 3.4 funktioniert.

+1

Denken Sie, dass sie den Paketnamen änderten ... – linusg

+2

Empfehlung: verwenden Sie einfach [Anfragen] (http://docs.python-requests.org/en/master/). – Kupiakos

+0

Mit Python 3.2.3 unter GNU/Linux funktioniert das gut. Vielleicht Windows-Kompilierungsproblem ... –

Antwort

0

Nach der Arbeit an der Lösung begann der Python 2.7-Code den gleichen Fehler zu werfen. Aus Frustration begann ich den Microsoft Fix anzuwenden. Ich begann mit dem Abmelden und erneutem Anmelden. Alles funktioniert jetzt wie geplant ...