Note: The timeout parameter does not prevent the request from loading forever, it only stops if the remote server fails to send response data within the timeout value. It could still load indefinitely.
Set the timeout parameter:
try:
r = requests.get("example.com", timeout=10) # 10 seconds
except requests.exceptions.Timeout:
print("Timed out")
The code above will cause the call to requests.get() to timeout if the connection or delays between reads takes more than ten seconds.
The timeout parameter accepts the number of seconds to wait as a float, as well as a (connect timeout, read timeout) tuple.
See requests.request documentation as well as the timeout section of the "Advanced Usage" section of the documentation.
Answer from Lukasa on Stack OverflowNote: The timeout parameter does not prevent the request from loading forever, it only stops if the remote server fails to send response data within the timeout value. It could still load indefinitely.
Set the timeout parameter:
try:
r = requests.get("example.com", timeout=10) # 10 seconds
except requests.exceptions.Timeout:
print("Timed out")
The code above will cause the call to requests.get() to timeout if the connection or delays between reads takes more than ten seconds.
The timeout parameter accepts the number of seconds to wait as a float, as well as a (connect timeout, read timeout) tuple.
See requests.request documentation as well as the timeout section of the "Advanced Usage" section of the documentation.
What about using 'eventlet'? If you want to timeout the request after 10 seconds, even if data is being received, this snippet will work for you:
import requests
import eventlet
eventlet.monkey_patch()
with eventlet.Timeout(10):
requests.get("http://ipv4.download.thinkbroadband.com/1GB.zip", verify=False)
Timeout in requests.get
Why Does My Python POST Request Keep Timing Out?
requests.get() timeout
Python Requests - Hanging Requests despite setting Timeout Value
off the top of my head i think the issue is that you are only handling a small set of exceptions. If the try never completes and it is not one of your defined exceptions it will never break.
I would add the a generic except at the end to show you what else it may be breaking. And I would but a finally statement after that to output something useful to see if it is looping are blocking.
More on reddit.comVideos
I’m testing an API using Python's requests library but keep running into a Timeout error: "Request timed out", even though the same setup works fine in Postman.
import requests
url = "https://api.example.com/endpoint"
headers = {
"Key1": "value1",
"Key2": "value2",
"Cookie": "CSToken=the actual token"
}
try:
response = requests.post(url, headers=headers, json=payload, timeout=10)
print(response.status_code)
print(response.text)
except requests.exceptions.Timeout:
print("Request timed out")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")Questions:
-
Could there be additional settings or implicit headers in Postman I’m missing in Python?
-
What steps can I take to debug or resolve this issue?
Any help is greatly appreciated. Thank you!
What I Tried: I sent a POST request to the API using Python's requests library, including the Cookie header with the authentication token, as done in Postman.
What I Expected: I expected the API to return the same successful response it does in Postman.
What Actually Happened: The request timed out, raising a Timeout exception in Python, even though it works without issues in Postman.