【问题标题】:Max retries exceeded with urlurl 超出了最大重试次数
【发布时间】:2020-08-18 09:07:11
【问题描述】:

我的 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'))]

【问题讨论】:

  • 请出示您的 urls.py
  • 你必须有证书才能使用HTTPS,你有吗?您可以为 localhost 创建一个,请参阅 Let's Encrypt 文档:letsencrypt.org/docs/certificates-for-localhost。另一个可以帮助您的工具是 ngrok,您可以使用 HTTP 和 HTTPS 为 localhost 创建一个隧道。

标签: python python-3.x django https python-requests


【解决方案1】:

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

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

有几种方法可以实现这一点,但我更喜欢使用来自django-extensionsRunServerPlus。他们有 this section 用于 SSL 设置。

你可以在this question找到一个很好的讨论。

【讨论】:

  • 我尝试安装 RunServerPlus 但出现以下错误:"from django.utils.autoreload import get_reloaderImportError: cannot import name 'get_reloader'" 当我使用此命令启动服务器时:./manage.py runserver_plus --cert /tmp/cert
  • 其实好像没有get_reloader()这个函数进入文件autoreload
  • 你使用的是什么版本的 Django?
猜你喜欢
  • 2013-08-30
  • 2021-02-03
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 2022-08-11
  • 2020-07-11
  • 2018-11-04
  • 2021-04-03
相关资源
最近更新 更多