首页 > 解决方案 > Python-Django [SSL: WRONG_VERSION_NUMBER] 错误

问题描述

当我尝试连接到我的本地主机时,我收到 [SSL: WRONG_VERSION_NUMBER] 错误。我默认使用“8080”端口。以前,我收到了 ProxyError,然后我将我的 url 从“http”更改为“https”,现在我收到了 SSLError。我检查了一些解决方案,提示更改端口号。它是否与端口号或其他任何事情有关?

视图.py:

endpoint = 'https://****:8080/MyApp/services/DBConnection/callLoginProcedure'

def index(request):
    post = request.POST
    if request.POST.get('login_button'):
        qd = QueryDict(mutable=True)
        qd.update(
            inputPhoneNumber=request.POST.get('phone_num'),
            inputPassword=request.POST.get('password')
        )
        response = requests.post('{}?{}'.format(endpoint, qd.urlencode()), verify=False)
        result = response.json()
        messages.info(request, result)

    return render(request, 'login/index.html')

错误如下

堆栈跟踪:

Django Version: 2.2.3
Python Version: 3.7.3
Installed Applications:
['login',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware']



Traceback:

File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  603.                                                   chunked=chunked)

File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py" in _make_request
  344.             self._validate_conn(conn)

File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py" in _validate_conn
  843.             conn.connect()

File "C:\Program Files\Python37\lib\site-packages\urllib3\connection.py" in connect
  370.             ssl_context=context)

File "C:\Program Files\Python37\lib\site-packages\urllib3\util\ssl_.py" in ssl_wrap_socket
  368.     return context.wrap_socket(sock)

File "C:\Program Files\Python37\lib\ssl.py" in wrap_socket
  412.             session=session

File "C:\Program Files\Python37\lib\ssl.py" in _create
  853.                     self.do_handshake()

File "C:\Program Files\Python37\lib\ssl.py" in do_handshake
  1117.             self._sslobj.do_handshake()

During handling of the above exception ([SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)), another exception occurred:

File "C:\Program Files\Python37\lib\site-packages\requests\adapters.py" in send
  449.                     timeout=timeout

File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  641.                                         _stacktrace=sys.exc_info()[2])

File "C:\Program Files\Python37\lib\site-packages\urllib3\util\retry.py" in increment
  399.             raise MaxRetryError(_pool, url, error or ResponseError(cause))

During handling of the above exception (HTTPSConnectionPool(****): Max retries exceeded with url: /MyApp/services/DBConnection/callLoginProcedure?inputPhoneNumber=231412&inputPassword=4211 (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)')))), another exception occurred:

File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\TOLGA\Desktop\PythonWebProjects\WebLogin\login\views.py" in index
  53.         response = requests.post('{}?{}'.format(endpoint, qd.urlencode()), verify=False)

File "C:\Program Files\Python37\lib\site-packages\requests\api.py" in post
  116.     return request('post', url, data=data, json=json, **kwargs)

File "C:\Program Files\Python37\lib\site-packages\requests\api.py" in request
  60.         return session.request(method=method, url=url, **kwargs)

File "C:\Program Files\Python37\lib\site-packages\requests\sessions.py" in request
  533.         resp = self.send(prep, **send_kwargs)

File "C:\Program Files\Python37\lib\site-packages\requests\sessions.py" in send
  646.         r = adapter.send(request, **kwargs)

File "C:\Program Files\Python37\lib\site-packages\requests\adapters.py" in send
  514.                 raise SSLError(e, request=request)

Exception Type: SSLError at /login/
Exception Value: HTTPSConnectionPool(***) (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)')))

标签: pythondjangosslhttpspython-requests

解决方案


endpoint = 'https://****:8080/MyApp/services/DBConnection/callLoginProcedure'

从您在上一个问题中的编辑中,可以检索到原始 URL。尝试此操作时,很明显您尝试访问的端点仅支持给定端口 8080 上的 HTTP,而不支持您尝试使用的 HTTPS。

[SSL:WRONG_VERSION_NUMBER] 错误

您看到的错误源于尝试使用只能执行 HTTP 的 HTTPS 访问站点。您的客户端通过发送 ClientHello 来启动 TLS 握手,并期望服务器回复 ServerHello。只是,服务器发送一个纯 HTTP 响应。然后,客户端尝试将此响应解释为 TLS ServerHello,其中包括从响应中特定位置的某些字节中找出 TLS 协议版本。由于这不是 TLS 响应,因此当解释为 TLS 时,这些信息毫无意义,这会导致这个奇怪的错误消息。

正确的方法是通过 HTTP 而不是 HTTPS 访问 URL。如果您在那里遇到问题(您提到一些没有详细信息的 ProxyError),那么您需要解决这些问题,而不仅仅是尝试通过 HTTPS 访问该站点 - 正如您所看到的那样只会导致其他问题。


推荐阅读