Reputation: 364
Is there a way to receive the status code of a request if the request throws an exception? I am trying to send a request, but there is a ConnectionTimeout. I've tried printing the error, but there's not useful status info in the error message. The actual object that is returned by requests.request() doesn't seem to get created when the exception is thrown, at least in my case. Could this be due to internal server configurations or something?
What I have attempted:
try:
response = requests.request(
method=some_http_method,
url=some_url,
auth=some_auth,
data=some_data,
timeout=30,
verify=some_certificate)
except requests.exceptions.ConnectTimeout as e:
response = e.response
print(e)
print(response.status_code) # should print a status code, instead response is still None
Upvotes: 0
Views: 404
Reputation: 417
The exception ConnectTimeout
means that the request timed out while trying to connect to the remote server. Thus, there's no response to get the status code from. it is normal that response
variable is None
It can be some network issue, make sure the URL you're trying to query is reachable (use curl
to verify)
Upvotes: 2