尝试在 Python 中为 API 'requests.get' 的许多错误

Posted

技术标签:

【中文标题】尝试在 Python 中为 API \'requests.get\' 的许多错误【英文标题】:Many errors trying to 'requests.get' for an API in Python尝试在 Python 中为 API 'requests.get' 的许多错误 【发布时间】:2021-11-15 07:39:00 【问题描述】:

我正在尝试学习使用 python 访问 API。 我什至都在努力克服第一道障碍。

按照我放的教程:

import requests

response = requests.get("https://api.open-notify.org/astros.json")

print(response.status_code)

使用了开放通知 api,因为它非常简单,有利于学习。

运行脚本后,我得到的只是本文底部显示的错误消息。

我发帖是希望有人以前遇到过这个问题并且知道解决方案,或者有人只是知道解决方案。

任何指导将不胜感激!

谢谢。

磅数

PS Microsoft.PowerShell.Core\FileSystem::\\[Comp\Users\[user name]\Documents\TRF_Python> & C:/Users/[user name]/AppData/Local/Programs/Python/Python38/python.exe //
[Computer name]/Users/[user name]/Documents/TRF_Python/Test_API_V1.py
Traceback (most recent call last):
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
    conn.connect()
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 309, in connect
    conn = self._new_conn()
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 171, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000019108B0F670>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 726, in urlopen
    retries = retries.increment(
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 439, 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: /astros.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000019108B0F670>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "//[Computer name]/Users/[user name]/Documents/TRF_Python/CompHous_API_V1.py", line 3, in <module>
    response = requests.get("https://api.open-notify.org/astros.json")
    return request('get', url, params=params, **kwargs)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.open-notify.org', port=443): Max retries exceeded with url: /astros.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000019108B0F670>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
PS Microsoft.PowerShell.Core\FileSystem::\\[Computer name]\Users\[user name]\Documents\TRF_Python> & C:/Users/[user name]/AppData/Local/Programs/Python/Python38/python.exe //
[Computer name]/Users/[user name]/Documents/TRF_Python/CompHous_API_V1.py
Traceback (most recent call last):
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
    conn.connect()
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 309, in connect
    conn = self._new_conn()
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 171, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000025AD018F730>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 726, in urlopen
    retries = retries.increment(
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 439, 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: /astros.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000025AD018F730>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "//[Computer name]/Users/[user name]/Documents/TRF_Python/CompHous_API_V1.py", line 3, in <module>
    response = requests.get("https://api.open-notify.org/astros.json", timeout=None)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\[user name]\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.open-notify.org', port=443): Max retries exceeded with url: /astros.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000025AD018F730>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
PS Microsoft.PowerShell.Core\FileSystem::\\[Computer name]\Users\[user name]\Documents\TRF_Python>

【问题讨论】:

有趣的是,我在本地得到这个:requests.exceptions.SSLError: HTTPSConnectionPool(host='api.open-notify.org', port=443): Max retries exceeded with url: /astros.json (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1122)'))) 【参考方案1】:

https://api.open-notify.org/astros.json 在您的浏览器中给出了类似的错误。问题是它不支持HTTPS。 http://api.open-notify.org/astros.json 有效。

这里有个问题:https://github.com/open-notify/Open-Notify-API/issues/9

【讨论】:

就我个人而言,我觉得这很多不太安全。任何 API 都应该强制使用 HTTPS

以上是关于尝试在 Python 中为 API 'requests.get' 的许多错误的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中为 Gmail API 创建消息

在 Python 中为 API 令牌生成 UUID

这是使用 API 在 Python 中为 Braintree 创建订阅的正确方法吗

使用 Python-oauth2 在 python 中为 tumblr API 初始化 Oauth 客户端

dict url的dict在Python中为post请求编码了params?

在WSO2中为每个租户配置动态端点