SSL错误(从Python 2.7.15到Python 3.7.7的端口)-HTTP错误401:消息验证失败
将代码从Python2.7.15移植到Python3.7.7,我在连接到内部服务器时遇到了让SSL调用正常工作的问题 在每个版本的Python中运行此代码,我得到: Python 2.7.15中的结果- (“服务器选择的版本:”,“TLSv1/SSLv3”) Python 3.7.7中的结果- 服务器选择的版本:TLSv1.2 此外,我正在为SSL调用设置上下文,如下所示:SSL错误(从Python 2.7.15到Python 3.7.7的端口)-HTTP错误401:消息验证失败,python,ssl,Python,Ssl,将代码从Python2.7.15移植到Python3.7.7,我在连接到内部服务器时遇到了让SSL调用正常工作的问题 在每个版本的Python中运行此代码,我得到: Python 2.7.15中的结果- (“服务器选择的版本:”,“TLSv1/SSLv3”) Python 3.7.7中的结果- 服务器选择的版本:TLSv1.2 此外,我正在为SSL调用设置上下文,如下所示: context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) 如何在3.7.7中设置上下文以
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
如何在3.7.7中设置上下文以满足服务器要求
谢谢你的帮助!
当做
迈克尔
Steffen,谢谢你的帮助!我花了时间调查并发布了调试日志。在Python 3.7中,urlIib2.open()中对self._open()的第一次调用返回一个错误处理程序对象,该对象包含401错误代码。
我不确定这是否有用——如果我需要提供其他信息,请让我知道……谢谢。
我有更多的信息和结构内容等,但不想压倒。请让我知道我错过了什么
我使用以下代码启用了日志记录:
import requests
import logging
from http.client import HTTPConnection # py3
log = logging.getLogger('urllib3')
log.setLevel(logging.DEBUG)
# logging from urllib3 to console
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
log.addHandler(ch)
# print statements from `http.client.HTTPConnection` to console/stdout
HTTPConnection.debuglevel = 1
requests.get('http://httpbin.org/')
以下是每个测试的日志信息:
Python 2.7.15
Starting new HTTP connection (1): httpbin.org:80
send: 'GET / HTTP/1.1\r\nHost: httpbin.org\r\nConnection: keep-
alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python- requests/2.19.1\r\n\r\n'
http://httpbin.org:80 "GET / HTTP/1.1" 200 9593
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Thu, 20 Aug 2020 19:53:40 GMT
header: Content-Type: text/html; charset=utf-8
header: Content-Length: 9593
header: Connection: keep-alive
header: Server: gunicorn/19.9.0
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Credentials: true
Python 3.7.7
Starting new HTTP connection (1): httpbin.org:80
send: b'GET / HTTP/1.1\r\nHost: httpbin.org\r\nUser-Agent: python- requests/2.22.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection:keep-alive\r\n\r\n'
http://httpbin.org:80 "GET / HTTP/1.1" 200 9593
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Thu, 20 Aug 2020 20:40:58 GMT
header: Content-Type: text/html; charset=utf-8
header: Content-Length: 9593
header: Connection: keep-alive
header: Server: gunicorn/19.9.0
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Credentials: true
exception HTTP Error 401: Message validation failed
我认为你把问题的原因看错了地方
因为您已经返回了代码401,这意味着收到了HTTP响应。这意味着,在HTTPS的情况下,TLS握手必须成功完成,否则就不会收到任何应用程序数据(因此也不会收到HTTP响应)。因此,问题很可能不是TLS握手,而是稍后的代码,您没有显示。我认为您在错误的地方寻找问题的原因 因为您已经返回了代码401,这意味着收到了HTTP响应。这意味着,在HTTPS的情况下,TLS握手必须成功完成,否则就不会收到任何应用程序数据(因此也不会收到HTTP响应)。因此,问题很可能不是TLS握手,而是后面的代码中没有显示的问题
Python 2.7.15
Starting new HTTP connection (1): httpbin.org:80
send: 'GET / HTTP/1.1\r\nHost: httpbin.org\r\nConnection: keep-
alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python- requests/2.19.1\r\n\r\n'
http://httpbin.org:80 "GET / HTTP/1.1" 200 9593
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Thu, 20 Aug 2020 19:53:40 GMT
header: Content-Type: text/html; charset=utf-8
header: Content-Length: 9593
header: Connection: keep-alive
header: Server: gunicorn/19.9.0
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Credentials: true
Python 3.7.7
Starting new HTTP connection (1): httpbin.org:80
send: b'GET / HTTP/1.1\r\nHost: httpbin.org\r\nUser-Agent: python- requests/2.22.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection:keep-alive\r\n\r\n'
http://httpbin.org:80 "GET / HTTP/1.1" 200 9593
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Thu, 20 Aug 2020 20:40:58 GMT
header: Content-Type: text/html; charset=utf-8
header: Content-Length: 9593
header: Connection: keep-alive
header: Server: gunicorn/19.9.0
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Credentials: true
exception HTTP Error 401: Message validation failed