【问题标题】:New to Python requests library, getting '[Errno 61] Connection refused'Python 请求库的新手,得到 \'[Errno 61] Connection refused\'
【发布时间】:2023-02-03 10:27:04
【问题描述】:

我到处搜索,找不到似乎适合我的用例的修复程序。我在我的 M1 mac 上遇到了一些 Python3 安装问题,但我认为它现在运行正常。

只是想学习/使用 requests library,但是当我尝试运行 this 时,非常简单的 python 代码:

import requests

response = requests.get("https://api.open-notify.org/this-api-doesnt-exist")
print(response.status_code)

我收到以下错误...

关于如何解决这个问题的任何建议?我正在拔头发。

谢谢!

/Users/myname/venv/python310/bin/python /Users/myname/Documents/2022_api/api01.py
Traceback (most recent call last):
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
    raise err
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connectionpool.py", line 1040, in _validate_conn
    conn.connect()
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connection.py", line 358, in connect
    conn = self._new_conn()
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x10f5a0850>: Failed to establish a new connection: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.open-notify.org', port=443): Max retries exceeded with url: /this-api-doesnt-exist (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x10f5a0850>: Failed to establish a new connection: [Errno 61] Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myname/Documents/2022_api/api01.py", line 3, in <module>
    response = requests.get("https://api.open-notify.org/this-api-doesnt-exist")
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.open-notify.org', port=443): Max retries exceeded with url: /this-api-doesnt-exist (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x10f5a0850>: Failed to establish a new connection: [Errno 61] Connection refused'))

Process finished with exit code 1

【问题讨论】:

  • 实际网站是http://api.open-notify.org/this-api-doesnt-exist,它不是https网站,而是http网站

标签: python python-3.x python-requests


【解决方案1】:

Connection Refused 表示主机 (api.open-notify.org) 没有监听您请求中的端口(https 在端口 443 上)。

我尝试连接到端口 80(纯 http),这对我有用:

>>> response = requests.get("http://api.open-notify.org/this-api-doesnt-exist")
>>> print(response.status_code)
404

为了笑,我尝试连接到基本网址

>>> response = requests.get("http://api.open-notify.org")
>>> print(response.status_code)
500
>>> response
<Response [500]>
>>> response.text
'<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
'

【讨论】:

  • 太感谢了!哇,这么简单。哈哈
【解决方案2】:

抱歉,但这并不能解决此处发布的问题。

当需要特定 URL 的特定数据时,将 URL 更改为基本 URI 并不能真正起作用。在这种情况下:“http://api.open-notify.org/this-api-doesnt-exist"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2016-10-12
    相关资源
    最近更新 更多