python requests请求失败重试_Python Requests.post()请求失败时的retry设置

1. 问题描述

通常,我们在做爬虫工作或远程接口调用的过程中,往往由于访问频率过快等原因遇到连接超时的报错问题,利用最近调用api.ai.qq.com某个接口举例如下:

Traceback (most recent call last):

: HTTPSConnectionPool(host='api.ai.qq.com', port=443):

Max retries exceeded with url: /fcgi-bin/nlp/nlp_textpolar (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 504 Gateway Time-out',)))

<==>用代码格式显示更为清晰:

Traceback (most recent call last):

: HTTPSConnectionPool(host='api.ai.qq.com', port=443):

Max retries exceeded with url: /fcgi-bin/nlp/nlp_textpolar (Caused by ProxyError('Cannot connect to proxy.',

OSError('Tunnel connection failed: 504 Gateway Time-out',)))

之前写爬虫时候为了快速解决问题就写了一堆的while与try来实现链接的重新访问,这种方式可以在一定程度上解决问题,但回看自己的代码时候总觉得奇丑无比,今天在做一个远程接口调用时候又遇到这个报错,想着python的requests模块中一定有内置方法来解决这个问题,于是便尝试了一下,但同时又引发了另一个小问题,遂决定记录一下。

2. 解决方案

其实,我们在http请求中遇到连接超时导致访问中断的情况时,往往不需要自己来写重访问策略,requests模块中有对应的重访问设置,可以通过设置最大重访问次数,当一次访问失败时,会自动进行重新访问,实现方式(简化)如下:

import requests

from requests.adapters import HTTPAdapter

from requests.packages.urllib3.util import Retry

s = requests.Session()

s.mount('https://', HTTPAdapter(max_retries=Retry(total=5)))

resp_get = s.get(url=http_url, data={'key':'value'})

resp_post = s.post(url=http_url, data={'key':'value'})

可以看到,我们设置最大重访问次数为5。但此时会发现,如果我们使用的是get()方法的话则没有问题,但如果使用post()方法的话仍然会报错。通过查资料发现,python的requests模块使用的urllib3,而urllib3默认对post()方法不设置重访问,因此需要我们手动指定才可以,修改如下:

import requests

from requests.adapters import HTTPAdapter

from requests.packages.urllib3.util import Retry

s = requests.Session()

s.mount('https://', HTTPAdapter(max_retries=Retry(total=5, method_whitelist=frozenset(['GET', 'POST'])))) # 设置 post()方法进行重访问

resp_get = s.get(url=http_url, data={'key':'value'})

resp_post = s.post(url=http_url, data={'key':'value'})

这样再使用requests.post()时几乎不会出现超时中断的情况了!

参考文献

https://stackoverflow.com/questions/35704392/how-to-make-python-post-requests-to-retry

https://www.biaodianfu.com/python-requests-retry.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值