47

In my code below, I use requests.post. What are the possibilities to simply continue if the site is down?

I have the following code:

def post_test():

    import requests

    url = 'http://example.com:8000/submit'
    payload = {'data1': 1, 'data2': 2}
    try:
        r = requests.post(url, data=payload)
    except:
        return   # if the requests.post fails (eg. the site is down) I want simly to return from the post_test(). Currenly it hangs up in the requests.post without raising an error.
    if (r.text == 'stop'):
        sys.exit()  # I want to terminate the whole program if r.text = 'stop' - this works fine.

How could I make the requests.post timeout, or return from post_test() if example.com, or its /submit app is down?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Zorgmorduk
  • 1,265
  • 2
  • 16
  • 32

2 Answers2

82

Use the timeout parameter:

r = requests.post(url, data=payload, timeout=1.5)

Note: timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds). If no timeout is specified explicitly, requests do not time out.

JacobIRR
  • 8,545
  • 8
  • 39
  • 68
  • are you still sure if no timeout is specified exlicitly, requests don't timeout. The thing is I tried it out and it seems they do timeout automatically ! – Haris Jul 27 '21 at 13:30
  • 1
    They don't time out because of Python, but because of the server timing out, which is outside of the control of your code. – JacobIRR Jul 27 '21 at 15:00
  • so you meant, the timeout will be counted from when the server received the request at the server side? OR is it counted from when client's requesting? I guess it was counted from when the server received the request. Thanks – turong Apr 12 '22 at 06:39
  • Its interesting: pylint gives you hint about timeout, and I wonder if socket-functions at all could have no timeout. Would mean a request will always timeout. – Nikolai Ehrhardt Oct 17 '22 at 16:16
28

All requests take a timeout keyword argument. 1

The requests.post is simply forwarding its arguments to requests.request 2

When the app is down, a ConnectionError is more likely than a Timeout. 3

try:
    requests.post(url, data=payload, timeout=5)
except requests.Timeout:
    # back off and retry
    pass
except requests.ConnectionError:
    pass
Josh Ackland
  • 603
  • 4
  • 19
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81