Python Requests - 无连接适配器
我正在使用Requests: HTTP for Humans这个库,遇到了一个奇怪的错误,我不知道这是什么意思。
No connection adapters were found for '192.168.1.61:8080/api/call'
有没有人知道这是怎么回事?
5 个回答
8
在我的情况下,我遇到了这个错误 是因为我在重构一个网址时,留下了一个多余的逗号,结果把我的网址从一个字符串变成了一个元组。
我收到的错误信息是:
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)
51
还有一个原因,可能是你的网址里包含了一些隐藏的字符,比如'\n'。
如果你像下面这样定义你的网址,就会出现这个异常:
url = '''
http://google.com
'''
因为字符串里隐藏着'\n'。实际上,网址变成了:
\nhttp://google.com\n
676
你需要加上协议的前缀:
'http://192.168.1.61:8080/api/call'
如果没有 http://
这一部分,requests
就不知道怎么去连接远程服务器。
注意,协议的前缀必须全部小写;比如如果你的网址是以 HTTP://
开头的,它也找不到 http://
的连接适配器。