首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >新加入Python请求库,获得“[Errno 61]连接被拒绝”

新加入Python请求库,获得“[Errno 61]连接被拒绝”
EN

Stack Overflow用户
提问于 2022-02-09 23:29:03
回答 1查看 3.3K关注 0票数 2

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

只是尝试学习/使用请求库,但是当我尝试运行时,非常简单的python代码:

代码语言:javascript
运行
复制
import requests

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

我知道下面的错误..。

关于如何解决这个问题有什么建议吗?我要把头发拔出来。

谢谢!

代码语言:javascript
运行
复制
/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-09 23:36:52

拒绝连接意味着主机(api.open-notify.org)没有侦听请求中的端口(https位于端口443)。

我尝试连接到端口80 (普通的http),这对我来说是有效的:

代码语言:javascript
运行
复制
>>> response = requests.get("http://api.open-notify.org/this-api-doesnt-exist")
>>> print(response.status_code)
404

为了好玩,我试着连接到基地url。

代码语言:javascript
运行
复制
>>> response = requests.get("http://api.open-notify.org")
>>> print(response.status_code)
500
>>> response
<Response [500]>
>>> response.text
'<html>\r\n<head><title>500 Internal Server Error</title></head>\r\n<body bgcolor="white">\r\n<center><h1>500 Internal Server Error</h1></center>\r\n<hr><center>nginx/1.10.3</center>\r\n</body>\r\n</html>\r\n'
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71058082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档