How to implement retry mechanism into python requests library?

前端 未结 6 2022
春和景丽
春和景丽 2020-12-02 10:12

I would like to add a retry mechanism to python request library, so scripts that are using it will retry for non fatal errors.

At this moment I do consider three kin

6条回答
  •  我在风中等你
    2020-12-02 10:33

    This is a snippet of code I used to retry for the petitions made with urllib2. Maybe you could use it for your purposes:

    retries = 1
    success = False
    while not success:
        try:
            response = urllib2.urlopen(request)
            success = True
        except Exception as e:
            wait = retries * 30;
            print 'Error! Waiting %s secs and re-trying...' % wait
            sys.stdout.flush()
            time.sleep(wait)
            retries += 1
    

    The waiting time grows incrementally to avoid be banned from server.

提交回复
热议问题