Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I receive a timeout error from Pythons requests module?

I use requests.post(url, headers, timeout=10) and sometimes I received a ReadTimeout exception HTTPSConnectionPool(host='domain.com', port=443): Read timed out. (read timeout=10)

Since I already set timeout as 10 seconds, why am I still receiving a ReadTimeout exception?

like image 814
chrizonline Avatar asked Feb 07 '15 01:02

chrizonline


People also ask

How do you fix timeout error in Python?

In Python, use the stdin. readline() and stdout. write() instead of input and print. Ensure that the input value to test cases is passed in the expected format.

What causes timeout error Python?

Timeouts in Python requestsIf the requests library does not receive response in x seconds, it will raise a Timeout error. It's a best practice that production code should use this parameter in all network requests. Failure to do so can cause your program to hang indefinitely.

What is Python requests default timeout?

The default timeout is None , which means it'll wait (hang) until the connection is closed.

How do you increase timeout in Python?

To set a timeout in Python Requests, you can pass the "timeout" parameter for GET, POST, PUT, HEAD, and DELETE methods. The "timeout" parameter allows you to select the maximum time (number of seconds) for the request to complete. By default, requests do not have a timeout unless you explicitly specify one.


1 Answers

Per https://requests.readthedocs.io/en/latest/user/quickstart/#timeouts, that is the expected behavior. As royhowie mentioned, wrap it in a try/except block (e.g.:

try:   requests.post(url, headers, timeout=10) except requests.exceptions.Timeout:   print "Timeout occurred" 

)

like image 83
Foon Avatar answered Sep 21 '22 00:09

Foon