我是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>
我已连接到我的大学网络。这就是我得到这个的原因吗?我该怎么办?
答案 0 :(得分:0)
MAX_RETRY = 5
def get_html(html_url, timeout=10, decode='utf-8'):
for tries in range(MAX_RETRY):
try:
with urllib.request.urlopen(html_url, timeout=timeout) as response:
return response.read().decode(decode)
except Exception as e:
logging.warning(str(e) + ',html_url:{0}'.format(html_url))
if tries < (MAX_RETRY - 1):
continue
else:
print('Has tried {0} times to access url {1}, all failed!'.format(MAX_RETRY, html_url))
return None
答案 1 :(得分:0)
另一个答案不专门处理超时异常情况。要处理超时情况:
let video_div = document.createElement('div');
video_div.id = 'video-div'
$(video_div).addClass('my-div')
$("body").append(($(video_div).draggable().resizable()))
let video_element;
video_element = document.createElement('video');
$(video_element).attr('id', 'my_video');
$(video_element).attr('class', 'video-js vjs-default-skin');
$(video_element).attr('width', '100%');
$(video_element).attr('height', '100%');
$(video_element).attr('controls', ' ');
$(video_element).attr('preload', 'auto');
$(video_element).attr('data-setup', '{}');
let source = document.createElement('source');
$(source).attr('type', "video/mp4");
$(source).attr('src', "http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4");
$(video_div).append(video_element)
$(video_element).append(source);