一段为 POST 获取数据的代码,
#! /usr/local/python3 import urllib import http import time import socket timeout = 10 socket.setdefaulttimeout(timeout) def up_post(username, password, page): data = {'username': username, 'password' : password} url = r"http://192.168.2.8/?page" + page postdata = bytes(urllib.parse.urlencode(data), encoding='gbk') respOnse= urllib.request.urlopen(url, data=postdata) text = response.read().decode('gbk') print(text) if text.find("成功") != -1: return page + ":" + text else: return page + ":error" username = "aaa" password = "bbb" page = 1 while page <= 99: try: static = up_post(username, password, page) except ( http.client.IncompleteRead, urllib.error.URLError, socket.timeout, ConnectionResetError) as e: time.sleep(5) static = up_post(username, password, page) while True: if static == page + ":" + static or static == page + ":error" or static == "": break
运行一段时间过后,会连续提示两个 timed out :
Traceback (most recent call last): File "mvc.py", line 103, in <module> static = up_post(username, password, page) File "mvc.py", line 27, in lan_up_post respOnse= urllib.request.urlopen(url, data=postdata) File "/usr/local/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/usr/local/lib/python3.6/urllib/request.py", line 526, in open respOnse= self._open(req, data) File "/usr/local/lib/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/usr/local/lib/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args) File "/usr/local/lib/python3.6/urllib/request.py", line 1346, in http_open return self.do_open( http.client.HTTPConnection, req) File "/usr/local/lib/python3.6/urllib/request.py", line 1321, in do_open r = h.getresponse() File "/usr/local/lib/python3.6/http/client.py", line 1331, in getresponse response.begin() File "/usr/local/lib/python3.6/http/client.py", line 297, in begin version, status, reason = self._read_status() File "/usr/local/lib/python3.6/http/client.py", line 258, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/local/lib/python3.6/socket.py", line 586, in readinto return self._sock.recv_into(b) socket.timeout: timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "mvc.py", line 106, in <module> static = up_post(username, password, page) File "mvc.py", line 27, in lan_up_post respOnse= urllib.request.urlopen(url, data=postdata) File "/usr/local/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/usr/local/lib/python3.6/urllib/request.py", line 526, in open respOnse= self._open(req, data) File "/usr/local/lib/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/usr/local/lib/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args) File "/usr/local/lib/python3.6/urllib/request.py", line 1346, in http_open return self.do_open( http.client.HTTPConnection, req) File "/usr/local/lib/python3.6/urllib/request.py", line 1321, in do_open r = h.getresponse() File "/usr/local/lib/python3.6/http/client.py", line 1331, in getresponse response.begin() File "/usr/local/lib/python3.6/http/client.py", line 297, in begin version, status, reason = self._read_status() File "/usr/local/lib/python3.6/http/client.py", line 258, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/local/lib/python3.6/socket.py", line 586, in readinto return self._sock.recv_into(b) socket.timeout: timed out
超时处理我只会 sleep 以后再重新运行一次获取代码,但是还是会出现两个 timed out 然后强行终止程序。
它提示 timed out 可以,但是我不想让它强行终止程序,我想让它不断重试!
咋搞啊?
![]() | 1 zeroten 2017-02-24 02:11:20 +08:00 via iPhone 把外部调用的函数加个装饰器,捕获异常并重试 |
![]() | 2 HFX3389 OP 这节点没有 APPEND 嘛? 我突然发现,似乎 if static == page + ":" + static or static == page + ":error" or static == "": break 并没有用啊。 加不加都会 timed out |
![]() | 3 argsno 2017-02-24 06:20:55 +08:00 via Android ![]() |
![]() | 4 kindjeff 2017-02-24 07:08:13 +08:00 via iPhone 循环 try catch 呗 |
![]() | 5 wannianma 2017-02-24 07:57:24 +08:00 via iPhone 最好设置个最高重试次数 |
![]() | 6 cszeus 2017-02-24 08:04:59 +08:00 用 2 层循环? while page <= 99: gotData = False reTry = 1 while reTry < 5 and not gotData: try: reTry += 1 static = up_post() gotData = True except: pass |
![]() | 7 rogerchen 2017-02-24 08:10:43 +08:00 via Android while try catch timeout break /td> |
8 herozhang 2017-02-24 08:11:56 +08:00 via iPhone 有个包叫做 retry ,可以试一下 |
![]() | 10 lerry 2017-02-24 09:29:30 +08:00 web 请求函数单独摘出来,手动捕获超时,超时就递归请求,正常就返回,还可以传一个最大尝试次数的参数 |
![]() | 11 awanabe 2017-02-24 10:32:17 +08:00 requests 库有 timeout 选项的 |
![]() | 12 Ge4Los 2017-02-24 10:49:16 +08:00 requests 有 timeout 和 retry 的配置。可以分别设置连接超时和读取超时。 |