gpt4 book ai didi

python - urllib.error.URLError :

转载 作者:行者123 更新时间:2023-12-04 17:58:09 50 4
gpt4 key购买 nike

我是 python 新手,正在使用 python 3.5.0。我试图实现一个简单的代码如下:

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>

我已连接到我的大学网络。这就是我得到这个的原因吗?我该怎么办?

最佳答案

另一个答案没有专门处理超时异常情况。处理超时情况:

try:
response = urllib.request.urlopen(request, timeout=10)
return response
except urllib.error.URLError as e:
if isinstance(e.reason, socket.timeout):
# handle timeout...
pass
raise e

关于 python - urllib.error.URLError : <urlopen error timed out>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38662135/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com