Sam
Sam

Reputation: 404

how to retry Http post requests with timeout

I am working on an API implementation where I would like to have a time limit for getting a response from a microservice and if the response is not received within the time limit then retry again for some more time(say 3 times)

import requests

url = 'http://example.com:8000/demo'
payload = {'data1': 1, 'data2': 2}
try:
    r = requests.post(url, data=payload)
except:
    return   # if the requests.post does not give response in a specific time-limit then I would like to retry request 3 times
    

Is there an inbuilt function in python where I can have a timeout and the retry the request for # times?

Upvotes: 0

Views: 1978

Answers (1)

Loïc
Loïc

Reputation: 11942

Ah you are doing very fine on the algorithm part.

Pretty sure you can build the loop yourself. I like to use range() in your case.

On the timeout part, requests has indeed your back :

https://requests.readthedocs.io/en/master/user/quickstart/#timeouts

Upvotes: 1

Related Questions