1

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'])
Relaxing
  • 33
  • 4
  • @jizhihaoSAMA I tried it, but it still returns NONE without retrying to connect, but I can use it like this in requests – Relaxing Apr 15 '21 at 17:30
  • @jizhihaoSAMA thanks,I found that I moved the import grequests to the top and it succeeded. thank you very much. – Relaxing Apr 15 '21 at 18:23

0 Answers0