0

I wrote a quick test for me to get a better understanding on the python request timeout. My understanding is that the timeout parameter is in seconds thus a timeout of 1 means, if the connection or the read time is more than 1 second an error should be thrown.

import requests
try:
    r = requests.get("https://stackoverflow.com/",timeout=0.1)
    print(r.elapsed)
except requests.exceptions.Timeout as e:
    print(e)

I first ran the script setting the timeout attribute as 1 to get a sense of how long it takes to reading from stackoverflow. My responses were just over 0.1 seconds. So naturally I reduced the timeout to 0.1 expecting an error to occur but instead, I still got the time elapsed printed.The printed time periods are all above 0.1

eg:

0:00:00.117071
0:00:00.118653
0:00:00.120222

What am I missing?

1

1 Answer 1

1

From the requests docs:

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.

The time you are printing is the total time of the process, but the timeout parameter concerns the time elapsed for the underlying socket in which no bytes were recieved as explained above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.