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?
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.
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.
The default timeout is None , which means it'll wait (hang) until the connection is closed.
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.
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"
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With