【问题标题】:requests python in Django ASGI server在 Django ASGI 服务器中请求 python
【发布时间】:2021-05-12 10:25:51
【问题描述】:

我正在使用 Django ASGI 服务器并正在发送一个发布请求,但它没有响应。 在邮递员中不断显示“发送请求” 我的意见.py

@csrf_exempt
def test(Request):
    if Request.method == "POST":
        response=requests.post('http://localhost:8000/api/token/')   
        return HttpResponse(response)

我的 urls.py

urlpatterns = [
    path('', include('chat.urls')),
    path('admin/', admin.site.urls),
    path('api-auth/', include('rest_framework.urls')),
    path('api/token/', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
]

这个请求很好 此请求无效

【问题讨论】:

  • 我建议将 print 放在视图 test() 中,以验证它是否被调用,并在 'if' 子句中进行验证。

标签: python django


【解决方案1】:

requests.post(...) 将转到执行当前请求的同一线程,因此等待执行并阻止 test() 继续并返回响应,因为您在同步函数中而不是异步中。 Requests 库不支持 async io ... 可以找 aiohttp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2013-05-20
    • 2019-08-02
    相关资源
    最近更新 更多