gpt4 book ai didi

python - 在 python http.client.HTTPSConnection 中设置更新的 ssl 版本

转载 作者:行者123 更新时间:2023-12-04 22:40:03 30 4
gpt4 key购买 nike

我有以下 https 客户端,它尝试将简单的 POST 请求发送到删除服务器。

!/usr/bin/python3

import http.client
import json
import ssl

context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

connection = http.client.HTTPSConnection('192.168.11.1', 444, context = context)

headers = {'Content-type': 'application/json',
'Accept': 'application/json',
'Content-Length': '0'}

foo = {'ver': '111'}
json_foo = json.dumps(foo)

connection.request('POST', '/post', json_foo, headers)

response = connection.getresponse()

print(response.read().decode())
但是,这样做时,我会得到 [SSL: WRONG_VERSION_NUMBER] wrong version number因此,我尝试将 ssl 协议(protocol)手动设置为较新的版本(在创建连接时使用 ssl.SSLContext)。
这是调用堆栈:
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 1245, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 1291, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 1240, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 1008, in _send_output
self.send(msg)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 948, in send
self.connect()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 1414, in connect
self.sock = self._context.wrap_socket(self.sock,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ssl.py", line 1040, in _create
self.do_handshake()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1108)
但是,在 python3 中,这是我拥有的选项。不幸的是,协议(protocol) 2 和 3 不在列表中。
PROTOCOL_SSLv23     
PROTOCOL_TLS_SERVER
PROTOCOL_TLSv1_2
PROTOCOL_TLS
PROTOCOL_TLSv1
PROTOCOL_TLS_CLIENT
PROTOCOL_TLSv1_1
知道如何在 http 连接中升级套接字的 ssl 版本吗?
我需要升级 ssl 包吗?我认为它在python3(3.8.2)中相对更新
谢谢 !

最佳答案

... it worked when i used ... curl ..."http://192.168.11.1:444/post"


您使用 curl 成功完成的是一个普通的 HTTP 请求( http://... 而不是 https://... )。您使用代码尝试(并且失败)的是 HTTPS 请求,即 HTTP over TLS。错误消息是尝试对不支持 TLS 的服务器使用 TLS 的结果。

关于python - 在 python http.client.HTTPSConnection 中设置更新的 ssl 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68116958/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com