Python 请求 - 无连接适配器
- 2025-03-06 08:52:00
- admin 原创
- 79
问题描述:
我正在使用请求:HTTP for Humans库,但出现了这个奇怪的错误,我不知道这是什么意思。
No connection adapters were found for '192.168.1.61:8080/api/call'
有人有想法吗?
解决方案 1:
您需要包含协议方案:
'http://192.168.1.61:8080/api/call'
如果没有该http://
部分,requests
就不知道如何连接到远程服务器。
请注意,协议方案必须全部小写;如果您的 URL 以HTTP://
例如开头,它也不会找到http://
连接适配器。
解决方案 2:
还有一个原因,也许您的网址包含一些隐藏字符,例如'\n'。
如果您像下面这样定义您的 url,则会引发此异常:
url = '''
http://google.com
'''
因为字符串中隐藏了 '\n'。url 实际上变成了:
http://google.com
解决方案 3:
就我而言,当我重构 url 时收到此错误,留下错误的逗号,从而将 url 从字符串转换为元组。
我的确切错误信息:
741 # Nothing matches :-/
--> 742 raise InvalidSchema("No connection adapters were found for {!r}".format(url))
743
744 def close(self):
InvalidSchema: No connection adapters were found for "('https://api.foo.com/data',)"
这个错误是这样产生的:
# Original code:
response = requests.get("api.%s.com/data" % "foo", headers=headers)
# --------------
# Modified code (with bug!)
api_name = "foo"
url = f"api.{api_name}.com/data", # !!! Extra comma doesn't belong here!
response = requests.get(url, headers=headers)
# --------------
# Solution: Remove erroneous comma!
api_name = "foo"
url = f"api.{api_name}.com/data" # No extra comma!
response = requests.get(url, headers=headers)
解决方案 4:
正如christian-long的评论所述
你的 url 可能会因为结尾的逗号而意外地变成一个元组
url = self.base_url % endpoint,
确保它是一个字符串
解决方案 5:
就我而言,问题在于我将 URL 定义为 .env 中环境变量的值,如下所示:
MY_ENDPOINT="<url>"
问题在于,至少 Docker 会将引号视为 URL 字符串的一部分,从而使 URL 无效。删除引号解决了我的问题:
MY_ENDPOINT=<url>
解决方案 6:
在我的例子中,我使用了默认url
方法
def call_url(url: str = "https://www.google.com"):
并且url
attr 被某种方法覆盖/some/url
解决方案 7:
我尝试json-server
通过请求进行调用,但无法调用 API。
根本问题是,它需要包括protocol scheme
iehttp://
import requests
url = 'http://localhost:3000/employees'
try:
response = requests.get(url)
if response:
print(response.json())
except requests.exceptions.RequestException as ex:
print(ex)
此外,对于http
请求 - 请求应该用try/except
块包裹 - 以便更好地调试问题的原因。
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD