2

I have something like this for requests error exception:

try:
    r = requests.get(url, stream=True)
    r.raise_for_status()
except requests.exceptions.HTTPError as err:
    print("HTTP exception error: {}".format(err))
    return
except requests.exceptions.RequestException as e:
    print("Exception error {}".format(e))
    return

To get an error like this I have to wait more than 2 minutes:

Exception error HTTPConnectionPool(host='192.168.137.67', port=8000): Max retries exceeded with url: /python-3.4.3.msi (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 110] Connection timed out',))

Is it possible to faster get timeout error? Try one or two times and then continue if something is wrong?

Hrvoje T
  • 3,365
  • 4
  • 28
  • 41

1 Answers1

4

timeout paramater:

r = requests.get(url, stream=True, timeout=10)

This will raise an exception if the request takes up than 10 seconds

More informations.

zamir
  • 2,144
  • 1
  • 11
  • 23
  • `timeout=10` will raise an error if the connection goes for more than 10 seconds without transferring any data. If a connection makes a little bit of progress every 5 seconds it might still take a request several minutes to complete. See [this answer](https://stackoverflow.com/a/8465202/456550) for an explanation and for some examples of how to set a timeout for the entire request. – Christian Long Aug 01 '18 at 04:30