Parse object content
I'm trying to parse an error that Requests returns when my test server is not started.
print("%sn" % type(error))
<class 'requests.exceptions.ConnectionError'>
print("%sn" % error)
HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /test_soap (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fecf0892a90>: Failed to establish a new connection: [Errno 111] Connexion refusée'))
When I open /usr/lib/python3.7/site-packages/requests/exceptions.py I can see the ConnectionError class has a response and request attributes:
print("%sn" % error.response)
None
print("%sn" % error.request)
<PreparedRequest [POST]>
But I would like to access to the urllib3.connection.HTTPConnection object so I can print the Failed to establish a new connection error message.
How exactly the requests.exceptions.ConnectionError class is built to show this error message when I print it?
python class
add a comment |
I'm trying to parse an error that Requests returns when my test server is not started.
print("%sn" % type(error))
<class 'requests.exceptions.ConnectionError'>
print("%sn" % error)
HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /test_soap (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fecf0892a90>: Failed to establish a new connection: [Errno 111] Connexion refusée'))
When I open /usr/lib/python3.7/site-packages/requests/exceptions.py I can see the ConnectionError class has a response and request attributes:
print("%sn" % error.response)
None
print("%sn" % error.request)
<PreparedRequest [POST]>
But I would like to access to the urllib3.connection.HTTPConnection object so I can print the Failed to establish a new connection error message.
How exactly the requests.exceptions.ConnectionError class is built to show this error message when I print it?
python class
You could try something likeexcept requests.exceptions.ConnectionError as e:and you could look at thetracebackmodule.
– connectyourcharger
Nov 13 '18 at 11:34
add a comment |
I'm trying to parse an error that Requests returns when my test server is not started.
print("%sn" % type(error))
<class 'requests.exceptions.ConnectionError'>
print("%sn" % error)
HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /test_soap (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fecf0892a90>: Failed to establish a new connection: [Errno 111] Connexion refusée'))
When I open /usr/lib/python3.7/site-packages/requests/exceptions.py I can see the ConnectionError class has a response and request attributes:
print("%sn" % error.response)
None
print("%sn" % error.request)
<PreparedRequest [POST]>
But I would like to access to the urllib3.connection.HTTPConnection object so I can print the Failed to establish a new connection error message.
How exactly the requests.exceptions.ConnectionError class is built to show this error message when I print it?
python class
I'm trying to parse an error that Requests returns when my test server is not started.
print("%sn" % type(error))
<class 'requests.exceptions.ConnectionError'>
print("%sn" % error)
HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /test_soap (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fecf0892a90>: Failed to establish a new connection: [Errno 111] Connexion refusée'))
When I open /usr/lib/python3.7/site-packages/requests/exceptions.py I can see the ConnectionError class has a response and request attributes:
print("%sn" % error.response)
None
print("%sn" % error.request)
<PreparedRequest [POST]>
But I would like to access to the urllib3.connection.HTTPConnection object so I can print the Failed to establish a new connection error message.
How exactly the requests.exceptions.ConnectionError class is built to show this error message when I print it?
python class
python class
asked Nov 13 '18 at 11:28
AdhrillAdhrill
31
31
You could try something likeexcept requests.exceptions.ConnectionError as e:and you could look at thetracebackmodule.
– connectyourcharger
Nov 13 '18 at 11:34
add a comment |
You could try something likeexcept requests.exceptions.ConnectionError as e:and you could look at thetracebackmodule.
– connectyourcharger
Nov 13 '18 at 11:34
You could try something like
except requests.exceptions.ConnectionError as e: and you could look at the traceback module.– connectyourcharger
Nov 13 '18 at 11:34
You could try something like
except requests.exceptions.ConnectionError as e: and you could look at the traceback module.– connectyourcharger
Nov 13 '18 at 11:34
add a comment |
1 Answer
1
active
oldest
votes
TL;DR You can't.
Not nicely at least.
The good thing is that you shouldn't, or at least you don't need to.
Consider:
import requests
try:
requests.get('http://localhost')
except Exception as error:
print(error)
print(type(error))
print([o for o in dir(error) if not o.startswith('__')])
outputs
# HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000019F34649C18>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
# <class 'requests.exceptions.ConnectionError'>
# ['args', 'characters_written', 'errno', 'filename',
# 'filename2', 'request', 'response',
# 'strerror', 'winerror', 'with_traceback']
If we look at the last output, nothing really stands out except for args:
print(error.args)
As usual, this will be a tuple that will contain the thrown object:
# (MaxRetryError("HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000189C4D7AD30>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))"),)
From here the way to the underlying urlib3 exception is ugly and messy, and does not really give us any new info we didn't already know:
print(error.args[0].reason.args[0])
# <urllib3.connection.HTTPConnection object at 0x0000023476C40390>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
Thanks it helps. Where do theargs,characters_written, ... come from? And how python knows what to print withprint(error)?
– Adhrill
Nov 13 '18 at 13:43
@Adhrill They are attributes of the exception object, andprint(error)prints the output oferror.__str__(), as usual.
– DeepSpace
Nov 13 '18 at 13:48
requests.exceptions.ConnectionErrorclass doesn't have a__str__method, so I believe it uses the__str__method from the defaultexceptionobject?
– Adhrill
Nov 13 '18 at 14:23
@AdhrillBaseException.__str__
– DeepSpace
Nov 13 '18 at 14:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280054%2fparse-object-content%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
TL;DR You can't.
Not nicely at least.
The good thing is that you shouldn't, or at least you don't need to.
Consider:
import requests
try:
requests.get('http://localhost')
except Exception as error:
print(error)
print(type(error))
print([o for o in dir(error) if not o.startswith('__')])
outputs
# HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000019F34649C18>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
# <class 'requests.exceptions.ConnectionError'>
# ['args', 'characters_written', 'errno', 'filename',
# 'filename2', 'request', 'response',
# 'strerror', 'winerror', 'with_traceback']
If we look at the last output, nothing really stands out except for args:
print(error.args)
As usual, this will be a tuple that will contain the thrown object:
# (MaxRetryError("HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000189C4D7AD30>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))"),)
From here the way to the underlying urlib3 exception is ugly and messy, and does not really give us any new info we didn't already know:
print(error.args[0].reason.args[0])
# <urllib3.connection.HTTPConnection object at 0x0000023476C40390>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
Thanks it helps. Where do theargs,characters_written, ... come from? And how python knows what to print withprint(error)?
– Adhrill
Nov 13 '18 at 13:43
@Adhrill They are attributes of the exception object, andprint(error)prints the output oferror.__str__(), as usual.
– DeepSpace
Nov 13 '18 at 13:48
requests.exceptions.ConnectionErrorclass doesn't have a__str__method, so I believe it uses the__str__method from the defaultexceptionobject?
– Adhrill
Nov 13 '18 at 14:23
@AdhrillBaseException.__str__
– DeepSpace
Nov 13 '18 at 14:24
add a comment |
TL;DR You can't.
Not nicely at least.
The good thing is that you shouldn't, or at least you don't need to.
Consider:
import requests
try:
requests.get('http://localhost')
except Exception as error:
print(error)
print(type(error))
print([o for o in dir(error) if not o.startswith('__')])
outputs
# HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000019F34649C18>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
# <class 'requests.exceptions.ConnectionError'>
# ['args', 'characters_written', 'errno', 'filename',
# 'filename2', 'request', 'response',
# 'strerror', 'winerror', 'with_traceback']
If we look at the last output, nothing really stands out except for args:
print(error.args)
As usual, this will be a tuple that will contain the thrown object:
# (MaxRetryError("HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000189C4D7AD30>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))"),)
From here the way to the underlying urlib3 exception is ugly and messy, and does not really give us any new info we didn't already know:
print(error.args[0].reason.args[0])
# <urllib3.connection.HTTPConnection object at 0x0000023476C40390>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
Thanks it helps. Where do theargs,characters_written, ... come from? And how python knows what to print withprint(error)?
– Adhrill
Nov 13 '18 at 13:43
@Adhrill They are attributes of the exception object, andprint(error)prints the output oferror.__str__(), as usual.
– DeepSpace
Nov 13 '18 at 13:48
requests.exceptions.ConnectionErrorclass doesn't have a__str__method, so I believe it uses the__str__method from the defaultexceptionobject?
– Adhrill
Nov 13 '18 at 14:23
@AdhrillBaseException.__str__
– DeepSpace
Nov 13 '18 at 14:24
add a comment |
TL;DR You can't.
Not nicely at least.
The good thing is that you shouldn't, or at least you don't need to.
Consider:
import requests
try:
requests.get('http://localhost')
except Exception as error:
print(error)
print(type(error))
print([o for o in dir(error) if not o.startswith('__')])
outputs
# HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000019F34649C18>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
# <class 'requests.exceptions.ConnectionError'>
# ['args', 'characters_written', 'errno', 'filename',
# 'filename2', 'request', 'response',
# 'strerror', 'winerror', 'with_traceback']
If we look at the last output, nothing really stands out except for args:
print(error.args)
As usual, this will be a tuple that will contain the thrown object:
# (MaxRetryError("HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000189C4D7AD30>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))"),)
From here the way to the underlying urlib3 exception is ugly and messy, and does not really give us any new info we didn't already know:
print(error.args[0].reason.args[0])
# <urllib3.connection.HTTPConnection object at 0x0000023476C40390>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
TL;DR You can't.
Not nicely at least.
The good thing is that you shouldn't, or at least you don't need to.
Consider:
import requests
try:
requests.get('http://localhost')
except Exception as error:
print(error)
print(type(error))
print([o for o in dir(error) if not o.startswith('__')])
outputs
# HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000019F34649C18>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
# <class 'requests.exceptions.ConnectionError'>
# ['args', 'characters_written', 'errno', 'filename',
# 'filename2', 'request', 'response',
# 'strerror', 'winerror', 'with_traceback']
If we look at the last output, nothing really stands out except for args:
print(error.args)
As usual, this will be a tuple that will contain the thrown object:
# (MaxRetryError("HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000189C4D7AD30>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))"),)
From here the way to the underlying urlib3 exception is ugly and messy, and does not really give us any new info we didn't already know:
print(error.args[0].reason.args[0])
# <urllib3.connection.HTTPConnection object at 0x0000023476C40390>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
edited Nov 13 '18 at 14:25
answered Nov 13 '18 at 11:42
DeepSpaceDeepSpace
38.6k44470
38.6k44470
Thanks it helps. Where do theargs,characters_written, ... come from? And how python knows what to print withprint(error)?
– Adhrill
Nov 13 '18 at 13:43
@Adhrill They are attributes of the exception object, andprint(error)prints the output oferror.__str__(), as usual.
– DeepSpace
Nov 13 '18 at 13:48
requests.exceptions.ConnectionErrorclass doesn't have a__str__method, so I believe it uses the__str__method from the defaultexceptionobject?
– Adhrill
Nov 13 '18 at 14:23
@AdhrillBaseException.__str__
– DeepSpace
Nov 13 '18 at 14:24
add a comment |
Thanks it helps. Where do theargs,characters_written, ... come from? And how python knows what to print withprint(error)?
– Adhrill
Nov 13 '18 at 13:43
@Adhrill They are attributes of the exception object, andprint(error)prints the output oferror.__str__(), as usual.
– DeepSpace
Nov 13 '18 at 13:48
requests.exceptions.ConnectionErrorclass doesn't have a__str__method, so I believe it uses the__str__method from the defaultexceptionobject?
– Adhrill
Nov 13 '18 at 14:23
@AdhrillBaseException.__str__
– DeepSpace
Nov 13 '18 at 14:24
Thanks it helps. Where do the
args, characters_written, ... come from? And how python knows what to print with print(error)?– Adhrill
Nov 13 '18 at 13:43
Thanks it helps. Where do the
args, characters_written, ... come from? And how python knows what to print with print(error)?– Adhrill
Nov 13 '18 at 13:43
@Adhrill They are attributes of the exception object, and
print(error) prints the output of error.__str__(), as usual.– DeepSpace
Nov 13 '18 at 13:48
@Adhrill They are attributes of the exception object, and
print(error) prints the output of error.__str__(), as usual.– DeepSpace
Nov 13 '18 at 13:48
requests.exceptions.ConnectionError class doesn't have a __str__ method, so I believe it uses the __str__ method from the default exception object?– Adhrill
Nov 13 '18 at 14:23
requests.exceptions.ConnectionError class doesn't have a __str__ method, so I believe it uses the __str__ method from the default exception object?– Adhrill
Nov 13 '18 at 14:23
@Adhrill
BaseException.__str__– DeepSpace
Nov 13 '18 at 14:24
@Adhrill
BaseException.__str__– DeepSpace
Nov 13 '18 at 14:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280054%2fparse-object-content%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You could try something like
except requests.exceptions.ConnectionError as e:and you could look at thetracebackmodule.– connectyourcharger
Nov 13 '18 at 11:34