一段为 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
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
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 可以,但是我不想让它强行终止程序,我想让它不断重试!
咋搞啊?