django - 在 django 的 asynchttpconsumer 中发送 http 请求?
问题描述
我的 django 项目中有一个 asynchttpconsumer 类,它从外部服务器接收 webhook。在 webhook 请求期间,我想向授权服务器发送 http 请求,获取响应,并使用它来完成 webhook 请求的处理。我遵循了本教程,但扩展了我拥有的代码。https://channels.readthedocs.io/en/latest/topics/consumers.html#asynchttpconsumer
class frontEndConsumer(AsyncHttpConsumer):
async def http_request(self, request):
header = self.scope
print(header)
# connect to auth server
conn = http.client.HTTPConnection("https://******.auth0.com", 80)
headers = { 'authorization': "Bearer *******************" }
print("request get connection")
conn.request("GET", "/userinfo", headers=headers)
res = conn.getresponse()
data = res.read()
# do some processing
print(data.decode("utf-8"))
# do other stuff
await self.http_reply(data)
return
由于我使用的是身份验证服务 (auth0),因此我使用他们的示例从这里获取信息:https ://auth0.com/docs/quickstart/backend/python/02-using 但我不断收到 errno -2 “conn.request”行:
Exception inside application: [Errno -2] Name or service not known
File "/home/jeremy/.local/lib/python3.6/site-packages/channels/consumer.py", line 59, in __call__
[receive, self.channel_receive], self.dispatch
File "/home/jeremy/.local/lib/python3.6/site-packages/channels/utils.py", line 51, in await_many_dispatch
await dispatch(result)
File "/home/jeremy/.local/lib/python3.6/site-packages/channels/consumer.py", line 73, in dispatch
await handler(message)
File "/home/jeremy/Documents/APbackend/myapp/webhook.py", line 59, in http_request
conn.request("GET", "/userinfo", headers=headers)
File "/usr/lib/python3.6/http/client.py", line 1264, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1310, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1259, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1038, in _send_output
self.send(msg)
File "/usr/lib/python3.6/http/client.py", line 976, in send
self.connect()
File "/usr/lib/python3.6/http/client.py", line 948, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.6/socket.py", line 704, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
[Errno -2] Name or service not known
我找不到对此错误的任何引用,或者这甚至是我正在尝试做的正确方法!
解决方案
弄清楚发生了什么:
我的网址应该没有 https://
在此之后我收到 301 错误
这是由 HTTPConnection 引起的,auth0 不支持 http,只支持 https。所以它需要更改为 HTTPSConnection 并将端口更改为 443。
之后能够让它工作。
推荐阅读
- c# - 如何旋转地球仪,以便在使用鼠标单击地球仪上选择位置时不要将地球仪侧向倾斜?
- javascript - 如何使用 JavaScript 获得焦点 HTML 元素的样式?
- html - type="number" 的上下箭头未检测到更改的值
- r - 基于 3 个变量的热图
- laravel - Laravel - 如何从角色表中设置的另一个表中获取权限行
- r - 通常在 R markdown 中禁用警告和消息传递
- airflow - Apache Airflow,获取和执行任务
- reactjs - 如何在 redux 存储中保存数组对象数据
- python - 如何在 Pandas 系列(或列表)的每个元素的特定字符后删除部分字符串?
- ruby-on-rails - Rails & Postgres:许多布尔关系条目的最佳实践