将代码从Python 2.7.15移植到Python 3.7.7,我无法让我们的SSL调用正常工作以连接到内部服务器。
在每个版本的Python中运行此代码,我得到:
import socket,ssl
context = context = ssl.create_default_context()
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
domain = 'xxx.yyy.com'
sslSocket = context.wrap_socket(s,server_hostname = domain)
sslSocket.connect((domain,443))
print('the selected version by the server: ',sslSocket.cipher()[1])
Python 2.7.15中的结果-
(“服务器选择的版本:”,“ TLSv1 / SSLv3”)
Python 3.7.7中的结果-
服务器选择的版本:TLSv1.2
此外,我为SSL调用设置了以下内容:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
如何在3.7.7中设置上下文以满足服务器的需求?
感谢您的帮助! 问候, 迈克尔
史蒂芬,谢谢您的帮助!我花了一些时间调查并发布了调试日志。在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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。