How can I get the http body when python3 throws a HTTPError
Asked Answered
D

2

9

How can I get the http body when python3.2 throws a HTTPError.

Traceback (most recent call last):
    File "C:\Users\RileyRen\Desktop\query_token.py", line 17, in <module>
    File "D:\Environment\Python32\lib\urllib\request.py", line 495, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 401: Unauthorized

python throws an HTTPError,But I can't get the http body.

Distaff answered 21/6, 2013 at 3:19 Comment(0)
C
15

Catch it and use it as if it were a response:

try:
    conn = urllib.request.urlopen(...)
    # ...
except urllib.error.HTTPError as error:
    data = error.read()
    # data contains the content
Cesaro answered 21/6, 2013 at 3:38 Comment(0)
R
2

just like the normal process.

err.read()
Risteau answered 21/6, 2013 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.