处理使用urllib时socket.timeout: timed out问题

import urllib.request,urllib.error
import socket

try:
    response = urllib.request.urlopen(url, timeout=5).read().decode('utf-8')
except urllib.error.HTTPError as error:
    logging.error('Data not retrieved because %s\nURL: %s', error, url)
except urllib.error.URLError as error:
    if isinstance(error.reason, socket.timeout):
        logging.error('socket timed out - URL %s', url)
    else:
        logging.error('some other error happened')
except socket.timeout as error: 
    logging.error('socket timed out - URL %s', url)    
else:
    logging.info('Access successful.')

Traceback (most recent call last): File "D:/pythonproject/Auto/forcast/Auto_forcast1.py", line 8, in <module> timeout=30) File "D:\pythonproject\HyperTS\venv\lib\site-packages\taosrest\__init__.py", line 44, in connect return TaosRestConnection(**kwargs) File "D:\pythonproject\HyperTS\venv\lib\site-packages\taosrest\connection.py", line 84, in __init__ timezone=self._timezone, File "D:\pythonproject\HyperTS\venv\lib\site-packages\taosrest\restclient.py", line 74, in __init__ self._taosd_token = self.get_taosd_token() File "D:\pythonproject\HyperTS\venv\lib\site-packages\taosrest\restclient.py", line 101, in get_taosd_token response = urlopen(self._login_url, timeout=self._timeout) File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 525, in open response = self._open(req, data) File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 543, in _open '_open', req) File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 1345, in http_open return self.do_open(http.client.HTTPConnection, req) File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 1320, in do_open r = h.getresponse() File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1321, in getresponse response.begin() File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 296, in begin version, status, reason = self._read_status() File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 257, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "C:\Users\阿东\AppData\Local\Programs\Python\Python37\lib\socket.py", line 589, in readinto return self._sock.recv_into(b) socket.timeout: timed out 进程已结束,退出代码1
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值