How does grequests do something like'requests' time out auto reconnect.
import time
import requests
from requests.adapters import HTTPAdapter
for i in range(1,1000):
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=3))
s.mount('https://', HTTPAdapter(max_retries=3))
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
}
url = 'https://google.com'
try:
r = s.post(url,headers=header, timeout=5)
print(r.text)
except requests.exceptions.RequestException as e:
print(e)
The above is the effect I got through the request, but that is because the request has been encapsulated.
How to use grequests to achieve, below is my grequests code.
import grequests
import requests
import time
import json
header = {
'Accept': '*/*',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
}
url = 'https://google.com'
# data = {'pouchNo': str(hhh).strip("['").strip("']"),
# 'year': '2021'}
req_list = [grequests.request("POST", url=url, headers=header, data={'pouchNo': str(i).strip("['").strip("']"),
'year': '2021'}) for i in a]
res_list = grequests.map(req_list)
for xxxxxx in res_list:
jsondata = json.loads(xxxxxx.content.decode())
try:
print(str(xxxxxx.request.body.replace("pouchNo=", "").replace("&year=2021", "")) + "\t" + jsondata[
'msg'] + "\t" + jsondata['data'])
except KeyError:
print(str(xxxxxx.request.body.replace("pouchNo=", "").replace("&year=2021", "")) + "\t" + jsondata[
'msg'])