grequest超时后如何使用grequest自动重新连接

问题描述

grequests 如何执行诸如“请求”超时自动重新连接之类的操作。

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)

以上是我通过请求得到的效果,但那是因为请求被封装了。

如何使用grequests来实现,下面是我的grequests代码

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,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=","")) + "\t" + jsondata[
                    'msg'])

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)