【问题标题】:Python requests detailed ConnectionError handlingPython 请求详细的 ConnectionError 处理
【发布时间】:2018-03-17 04:08:43
【问题描述】:

我刚刚写了这个:

try:
    r = requests.get('http://example.com')
except requests.exceptions.ConnectionError as e:
    print(e)

我得到了这个输出:

('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

有谁知道我怎样才能得到不同类型的连接错误?像此异常中的“连接中止”、“连接被拒绝”和“连接重置”并处理它们?

【问题讨论】:

    标签: python exception python-requests


    【解决方案1】:

    如果您的目标是获取响应消息然后处理它们。你可以试试这段代码。

    import requests
    response = requests.get("http://www.example.com")
    print(response.status_code)
    print(response.reason)
    

    【讨论】:

    • 对不起,但我的意思是在 ConnectionError 类中处理我感觉的子错误。发生连接错误时,您无法获取 HTTP 响应代码,对吗?
    猜你喜欢
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多