0

I am using Python3 requests module, doing a try/except and catching a timeout error specifically. The except statement returns the exception as a class object. How can I parse out particular elements of the error, for example, the URL? This is Python 3.7.8 on Ubuntu 18.04 if that matters.

try:
    result = requests.get(domain, timeout=5)
except requests.exceptions.Timeout as Err:
    print(type(Err))
    print(Err)

Returns --

<class 'requests.exceptions.ConnectTimeout'>
HTTPSConnectionPool(host='10.10.10.92', port=443): Max retries exceeded with url: /custom.php (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7c16f14f73d1>, 'Connection to 10.10.10.92 timed out. (connect timeout=5)'))

I'd like to be able to grab just the "Max retries exceeded with url: /custom.php" section.

2
  • Does this answer your question? Python: Error message parsing Commented Aug 22, 2022 at 20:20
  • Not really, though it did give me something to look at that I had missed. I guess I could turn the whole thing into a string then use substring operations to extract the part I wanted. I might give that a try. Commented Aug 22, 2022 at 20:30

1 Answer 1

1

Timeout inherits from RequestException, which has a request property. So you don't have to parse anything, the url is available via (in your example) Err.request.url. The request is of type requests.Request.

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.