在 Python 中使用 HTTP 客户端库时遇到错误
Encountered the error when using HTTP client library in Python
我正在使用 HTTP 客户端库向服务器发送 RESTful GET。这是我的代码:
import http.client
conn = http.client.HTTPSConnection("")
headers = { 'authorization': "Bearer eyJ0eXAiOiJKV1QiLCJhbG.....JNa0UyT" }
conn.request("GET", "https://xxxx-xxxx.auth0.com/api/v2/jobs/job_xxxxxx", headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
这是一个错误:
Traceback (most recent call last):
File "getResult.py", line 4, in <module>
conn.request("GET", "https://xxxxxx.auth0.com/api/v2/jobs/job_xxxxxx", headers=headers)
File "/usr/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/usr/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/usr/lib/python3.5/http/client.py", line 1252, in connect
super().connect()
File "/usr/lib/python3.5/http/client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.5/socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.5/socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
我不知道为什么。我能帮忙吗?
查看 official docs,您应该使用主机名和端口实例化连接,然后仅使用端点字符串执行 GET。
您实际上是在询问服务器“”资源“https://xxxx-xxxx.auth0.com/api/v2/jobs/job_xxxxxx”
conn = http.client.HTTPSConnection("https://xxxx-xxxx.auth0.com")
headers = { 'authorization': "Bearer eyJ0eXAiOiJKV1QiLCJhbG.....JNa0UyT" }
conn.request("GET", "/api/v2/jobs/job_xxxxxx", headers=headers)
无论如何,我建议您使用 requests 进行 HTTP 调用。在这种情况下,它是事实上的图书馆。
我正在使用 HTTP 客户端库向服务器发送 RESTful GET。这是我的代码:
import http.client
conn = http.client.HTTPSConnection("")
headers = { 'authorization': "Bearer eyJ0eXAiOiJKV1QiLCJhbG.....JNa0UyT" }
conn.request("GET", "https://xxxx-xxxx.auth0.com/api/v2/jobs/job_xxxxxx", headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
这是一个错误:
Traceback (most recent call last):
File "getResult.py", line 4, in <module>
conn.request("GET", "https://xxxxxx.auth0.com/api/v2/jobs/job_xxxxxx", headers=headers)
File "/usr/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/usr/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/usr/lib/python3.5/http/client.py", line 1252, in connect
super().connect()
File "/usr/lib/python3.5/http/client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.5/socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.5/socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
我不知道为什么。我能帮忙吗?
查看 official docs,您应该使用主机名和端口实例化连接,然后仅使用端点字符串执行 GET。 您实际上是在询问服务器“”资源“https://xxxx-xxxx.auth0.com/api/v2/jobs/job_xxxxxx”
conn = http.client.HTTPSConnection("https://xxxx-xxxx.auth0.com")
headers = { 'authorization': "Bearer eyJ0eXAiOiJKV1QiLCJhbG.....JNa0UyT" }
conn.request("GET", "/api/v2/jobs/job_xxxxxx", headers=headers)
无论如何,我建议您使用 requests 进行 HTTP 调用。在这种情况下,它是事实上的图书馆。