Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 请求是否使重试方法在几秒钟后执行?_Python_Python Requests - Fatal编程技术网

Python 请求是否使重试方法在几秒钟后执行?

Python 请求是否使重试方法在几秒钟后执行?,python,python-requests,Python,Python Requests,这是我的密码 import requests,time proxies = {'http':'36.33.1.177:21219'} url='http://218.94.78.61:8080/newPub/service/json/call?serviceName=sysBasicManage&methodName=queryOutputOtherPollutionList&paramsJson=%7B%22ticket%22:%22451a9846-058b-4944-86c

这是我的密码

import requests,time
proxies = {'http':'36.33.1.177:21219'}
url='http://218.94.78.61:8080/newPub/service/json/call?serviceName=sysBasicManage&methodName=queryOutputOtherPollutionList&paramsJson=%7B%22ticket%22:%22451a9846-058b-4944-86c6-fccafdb7d8d0%22,%22parameter%22:%7B%22monitorSiteType%22:%2202%22,%22enterpriseCode%22:%22320100000151%22,%22monitoringType%22:%222%22%7D%7D'

i = 0
a = requests.adapters.HTTPAdapter(max_retries=10)
s = requests.Session()
s.mount(url, a)
for x in xrange(1,1000):
    time.sleep(1)
    print x
    try:
        r= s.get(url,proxies=proxies)
        print r
    except Exception as ee:
        i = i + 1
        print ee
        print 'i=%s' % i

代理有点不稳定,所以我设置了max_retries,但有时仍然会出现异常,所以在每次重试的几秒钟后是否有一些方法可以执行???

仅使用
请求
库是不可能的。但是,您可以像这样使用外部库

backoff
提供了一个装饰器,您可以将其包装在函数中。示例代码:

@backoff.on_exception(backoff.constant,
                      requests.exceptions.RequestException,
                      max_tries=10, interval=10)
def get_url(url):
    return requests.get(url)

上面的代码等待10秒钟,以便下次重试
请求的每个异常。exceptions.RequestException
,并按照
max\u trys
中的规定尝试了10次。对不起,我忘了。我只使用了stackoverflow几天。再次感谢。你能帮我回答这个问题吗:我查过了,对不起,我真的不知道是什么问题。