Hrvoje T
Hrvoje T

Reputation: 3913

How to wait less for connection timeout in Python3 requests?

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?

Upvotes: 2

Views: 4052

Answers (1)

zamir
zamir

Reputation: 2522

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.

Upvotes: 4

Related Questions