2016-02-29 50 views
7

我试图在Python中实现请求重试。
它的工作方式与.get()请求相似,但.post()请求不会重试,无论状态码如何。我想用.post()请求来使用它。如何使python .post()请求重试?

我的代码:

from requests.packages.urllib3.util import Retry 
from requests.adapters import HTTPAdapter 
from requests import Session, exceptions 

s = Session() 
s.mount('http://', HTTPAdapter(max_retries=Retry(total=2, backoff_factor=1, status_forcelist=[ 500, 502, 503, 504, 521]))) 
r = s.get('http://httpstat.us/500') 
r2 = s.post('http://httpstat.us/500') 

因此,.get()请求重试做和.post()那些没有。

怎么了?

+1

难道这就是像你期望的工作? GET请求不会损害数据,但可能会有多个“POST”。我还没有通读请求API文档,但这听起来是合理的,如果这是设计。 –

回答