首页 > 解决方案 > url 超出了最大重试次数

问题描述

我的 Django 代码有问题:

我试过这个:

requests.post('https://localhost:8000/api/test/', data=data, headers={'Content-Type': 'application/json'}, verify=False)

但我得到了这个:

{SSLError}HTTPSConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /api/test/ (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)'),))

我实现了使用 HTTP 而不是 https 来解决问题,但我想使用 https。

知道所有这些都在本地主机上,我怎么能做到这一点?

非常感谢!

编辑 :

这是来自 api 应用程序的 urls.py :

from django.urls import path, include
from django.views.decorators.csrf import csrf_exempt
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token

from API import views as API views

app_name = 'api'


from API.views import LoginViewCustom

urlpatterns = [
path('test/', apiviews.Test.as_view(), name='test')
]

在其他 urls.py 中:

urlpatterns = [path('api/', include('api.urls', namespace='api'))]

标签: pythonpython-3.xdjangohttpspython-requests

解决方案


Django 的开发服务器,默认情况下通过 http 运行应用程序。因此,Max retries exceeded with url当您尝试通过 https 访问它时,您会遇到错误。

因此,您真正想要的是使用 SSL/HTTPS 测试/运行本地开发服务器。

有几种方法可以实现这一点,但我更喜欢使用django -extensions 的RunServerPlus。他们有这个部分用于 SSL 设置。

你可以在这个问题中找到一个很好的讨论。


推荐阅读