Python 客户端错误 'Connection reset by peer'

Python client error 'Connection reset by peer'

我写了一个非常小的 python 客户端来访问 confluence restful api。我正在使用 https 协议连接 confluence。我 运行 进入 Connection reset by peer 错误。 这是完整的堆栈跟踪。

/Users/rakesh.kumar/.virtualenvs/wpToConfluence.py/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/Users/rakesh.kumar/.virtualenvs/wpToConfluence.py/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
Traceback (most recent call last):
  File "wpToConfluence.py", line 15, in <module>
    main()
  File "wpToConfluence.py", line 11, in main
    headers={'content-type': 'application/json'})
  File "/Users/rakesh.kumar/.virtualenvs/wpToConfluence.py/lib/python2.7/site-packages/requests/api.py", line 71, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/rakesh.kumar/.virtualenvs/wpToConfluence.py/lib/python2.7/site-packages/requests/api.py", line 57, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/rakesh.kumar/.virtualenvs/wpToConfluence.py/lib/python2.7/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/rakesh.kumar/.virtualenvs/wpToConfluence.py/lib/python2.7/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "/Users/rakesh.kumar/.virtualenvs/wpToConfluence.py/lib/python2.7/site-packages/requests/adapters.py", line 453, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))

这是我的客户端代码:

import requests


def main():
    auth = open('/tmp/confluence', 'r').readline().strip()

    username = 'rakesh.kumar'

    response = requests.get("https://<HOST-NAME>/rest/api/content/",
                            auth=(username, auth),
                            headers={'content-type': 'application/json'})
    print response

if __name__ == "__main__":
    main()

我在虚拟环境中运行这个脚本,并且在该环境中安装了以下软件包:

(wpToConfluence.py)➜  Python pip list
You are using pip version 6.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
appnope (0.1.0)
backports.shutil-get-terminal-size (1.0.0)
decorator (4.0.10)
ipdb (0.10.1)
ipython (5.0.0)
ipython-genutils (0.1.0)
pathlib2 (2.1.0)
pexpect (4.2.0)
pickleshare (0.7.3)
pip (6.1.1)
prompt-toolkit (1.0.5)
ptyprocess (0.5.1)
Pygments (2.1.3)
requests (2.10.0)
setuptools (25.1.6)
simplegeneric (0.8.1)
six (1.10.0)
traitlets (4.2.2)
urllib3 (1.16)
wcwidth (0.1.7)

它确实抱怨 python 版本号,但我不确定如何更新我的 Mac/Virtual 环境 python。

我已经尝试使用 curl 命令和 Postman,它们都可以在给定的参数下正常工作。

在安装 requests 库时,它会跳过一些 optional security packages('pyOpenSSL'、'ndg-httpsclient' 和 'pyasn1'),而这些 [=21] 是必需的=] 连接。 您可以通过 运行 此命令

来修复它
pip install "requests[security]"

pip install pyopenssl ndg-httpsclient pyasn1

我尝试安装上面答案中提供的所有可选安全包。但似乎没有任何效果。

一个重要的 GOTCHA:检查您的 url 端点是否主动阻止编程访问。

看一下网站根目录下的robots.txt文件: http://myweburl.com/robots.txt.

如果它包含如下所示的文本:用户代理:* 不允许:/

这个网站不喜欢也不想抓取。这会给您带来同样可怕的错误 54,连接被对等方重置。

这是一张快照:

https://www.aclibrary.org/robots.txt

用户代理:discobot 禁止:/ 用户代理:AddThis.com 禁止:/ 用户代理:Yandex 禁止:/ 用户代理:Baiduspider 禁止:/ 用户代理:Baiduspider-video 禁止: / 用户代理:Baiduspider-image 禁止:/ 用户代理:SemrushBot 禁止:/ 用户代理:SemrushBot-SA 禁止:/ 用户代理:W3C-checklink 抓取延迟:0 用户代理:Twitterbot 禁止:用户-代理:* 抓取延迟:10 禁止:/er.php 禁止:/err.php 禁止:/go.php 禁止:/friendly.php 禁止:/ld.php 禁止: /srch.php 站点地图:https://aclibrary.org/sitemap.xml

安装可选的安全包对我不起作用,以防万一有人收到相同的错误消息,请检查您尝试使用的 url 是否正确。

例如,如果服务器期望在不同的端口上建立连接,您可能会发现此错误。

我遇到了同样的问题,但安装可选的安全包对我也不起作用。

我不得不将 requests 版本从 2.25.1 降级到 2.21.0