简体   繁体   English

如何从请求响应中提取 HTTP 错误文本?

[英]How to extract the HTTP error text from a requests response?

I have the following code:我有以下代码:

    tries = 10
    for n in range(tries):
        try:
            ....
            responsedata = requests.get(url, data=data, headers=self.hed, verify=False)
            responsedata.raise_for_status()
            ..
            if .... : 
                break   #exit loop condition

        except (ChunkedEncodingError, requests.exceptions.HTTPError) as e:
            print ("page #{0} run #{1} failed. Returned status code {2}. Msg: {3}. Retry.".format(page, n, responsedata.status_code, sys.exc_info()[0]))
            if n == tries - 1:
               raise e  # exit the process

The prints I see are:我看到的印刷品是:

page #53 run #0 failed. Returned status code 502. Msg: <class 'requests.exceptions.HTTPError'>. Retry.
page #1 run #1 failed. Returned status code 500. Msg: <class 'requests.exceptions.ChunkedEncodingError'>. Retry.

While this is Ok it doesn't give me actual information about the problem.虽然这很好,但它并没有给我关于这个问题的实际信息。 The message just tell me the exception title.该消息只是告诉我异常标题。

If I print the: responsedata.text when exception happens I see:如果我在异常发生时打印: responsedata.text ,我会看到:

 Returned status code 502. Message is: ...
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>502 - Web server received an invalid response while acting as a gateway or proxy server.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
...

This is a giant message most of it is garbage but it also says: 502 - Web server received an invalid response while acting as a gateway or proxy server.这是一个巨大的消息,其中大部分是垃圾,但它也说: 502 - Web server received an invalid response while acting as a gateway or proxy server. can I access this message and also print it to my log?我可以访问此消息并将其打印到我的日志中吗?

您可以使用responsedata.status_code及其文本描述通过responsedata.reason访问响应的状态代码(请参阅http://docs.python-requests.org/en/master/api/中的更多内容)

If your endpoint returns a detailed, application-specific error message in the response body, you can make use of the fact that a requests HTTPError retains a reference to the Response that caused it to be raised:如果您的端点在响应正文中返回详细的、特定于应用程序的错误消息,您可以利用以下事实:请求HTTPError保留对导致它被引发的Response的引用:

from requests.exceptions import HTTPError

try:
    # Some code that makes requests
except HTTPError as e:
    print(e.response.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM