0
我是新來的蟒蛇和我使用python 3.5.0。我試圖實現一個簡單的代碼如下:蟒蛇 - urllib.error.URLError:<urlopen錯誤超時>
import urllib.request
page = urllib.request.urlopen("http://www.google.com",timeout=20)
text = page.read().decode("utf8")
print(text)
但沒想到我居然也得到了以下錯誤:
Traceback (most recent call last):
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1240, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1083, in request
self._send_request(method, url, body, headers)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1128, in _send_request
self.endheaders(body)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1079, in endheaders
self._send_output(message_body)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 911, in _send_output
self.send(msg)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 854, in send
self.connect()
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 826, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 707, in create_connection
raise err
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 698, in create_connection
sock.connect(sa)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python\Test_Run.py", line 2, in <module>
page = urllib.request.urlopen("http://www.google.com",timeout=20)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 465, in open
response = self._open(req, data)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 483, in _open
'_open', req)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
result = func(*args)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1268, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1242, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error timed out>
我連接到我的大學網絡。這是爲什麼我得到這個?我能做些什麼呢?
我在家用電腦上試過了你的代碼,它工作正常。問題應該在你的學校網絡中。嘗試它沒有'超時= 20' – atayenel
是的,我試過了,沒有超時。但這需要更長的時間,並且會出現相同的錯誤。 –
我猜你的瀏覽器被配置爲使用代理服務器,它可以讓你聯繫谷歌,但直接的http連接可能不被允許(即它們在某個時候被防火牆丟棄)...... – twalberg