暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

requests.get返回Remote end closed connection without response错误解决

一点鑫得 2023-03-18
4615

近期遇到一个问题使用python requests.get发送http请求,有些url直接报错,返回requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')),但是浏览器却可以正常打开网页,这是怎么回事呢?

报错信息详情如下:

    r=requests.get("http://www.xxz.gov.cn/2018zhuanti/jczwgkzl/bmfl.html?Parent=14")
    Traceback (most recent call last):
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
    File "<string>", line 3, in raise_from
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1347, in getresponse
    response.begin()
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin
    version, status, reason = self._read_status()
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 276, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
    http.client.RemoteDisconnected: Remote end closed connection without response


    During handling of the above exception, another exception occurred:


    Traceback (most recent call last):
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 440, in send
    resp = conn.urlopen(
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
    retries = retries.increment(
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
    raise value.with_traceback(tb)
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
    File "<string>", line 3, in raise_from
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1347, in getresponse
    response.begin()
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin
    version, status, reason = self._read_status()
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 276, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
    urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))


    During handling of the above exception, another exception occurred:


    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
    File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
    requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

    遇到这种情况,最先需要考虑请求头User-Agent,User-Agent是一个字符串,它描述了发送请求的客户端。如果服务器检测到User-Agent不是浏览器,它可能会关闭连接。默认情况下requests的User-Agent为'python-requests/2.27.1',既然浏览器可以访问,那么就可以尝试通过修改User-Agent解决这个问题。

      >>> r=requests.get("http://www.xxz.gov.cn/2018zhuanti/jczwgkzl/bmfl.html?Parent=14",headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41"})
      >>> r
      <Response [200]>
      >>> r.request.headers
      {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41', 'Accept-Encoding': 'gzip, deflate, br', 'Accept': '*/*', 'Connection': 'keep-alive'}

      文章转载自一点鑫得,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论