相关疑难解决方法(0)

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

我有以下代码:

    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
Run Code Online (Sandbox Code Playgroud)

我看到的照片是:

page #53 run #0 failed. Returned status code 502. Msg: <class 'requests.exceptions.HTTPError'>. Retry.
page #1 run #1 failed. Returned status code …
Run Code Online (Sandbox Code Playgroud)

python python-requests

3
推荐指数
2
解决办法
2763
查看次数

使用Python标准包将HTTP响应代码转换为文本消息

这个问题是关于HTTP 响应代码的。

在我的 python 应用程序中,我想向用户展示与此类代码相关的文本。例如404将是Not Found

我检查了 python 文档,但找不到为我提供代码文本/字符串的包。python 库中真的没有这样的东西吗?

解决方法是使用外部源。例如,来自 IANA 的官方 CSV 文件。

python http http-headers python-3.x

3
推荐指数
1
解决办法
1360
查看次数

标签 统计

python ×2

http ×1

http-headers ×1

python-3.x ×1

python-requests ×1