gpt4 book ai didi

python - 代码适用于 Python 3.6,但不适用于 3.7

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:13 26 4
gpt4 key购买 nike

那些日子我开始学习 python 和套接字,我制作了一个简单的客户端-服务器应用程序只是为了进行一些测试,它在 Python 3.6 上运行良好,但在最新版本中出现错误。

服务器.py

import socket, ssl

context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.verify_mode = ssl.CERT_REQUIRED
context.load_cert_chain(certfile="SSL/server.crt", keyfile="SSL/server.key")
context.load_verify_locations("SSL/client.crt")

bindsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
bindsocket.bind(("127.0.0.1", 65405))
bindsocket.listen(10)

while True:
newsocket, fromaddr = bindsocket.accept()
print(newsocket)
connstream = context.wrap_socket(newsocket, server_side=True)
try:
print(connstream.getpeercert())
finally:
connstream.shutdown(socket.SHUT_RDWR)
connstream.close()

客户端.py

import ssl, socket

context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
context.load_cert_chain(certfile="SSL/client.crt", keyfile="SSL/client.key")
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
context.load_verify_locations("SSL/server.crt")

conn = context.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM), server_hostname="127.0.0.1")

try:
conn.connect(("127.0.0.1", 65405))
conn.sendall(b"aff")
print(conn.getpeercert())
except:
print("não")
finally:
conn.shutdown(socket.SHUT_RDWR)
conn.close()

错误

Traceback (most recent call last):
File "C:/Users/nicol/Desktop/Kyuu/Bot/Kaori/Python/server.py", line 15, in <module>
connstream = context.wrap_socket(newsocket, server_side=True)
File "C:\Users\nicol\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 412, in wrap_socket
session=session
File "C:\Users\nicol\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 853, in _create
self.do_handshake()
File "C:\Users\nicol\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 1117, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_BAD_CERTIFICATE] sslv3 alert bad certificate (_ssl.c:1056)

编辑 1

我尝试使用 ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) 和 ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT),但我遇到了同样的错误。

编辑2

当我在 OpenSSL 中使用命令“s_client -connect 127.0.0.1:65405 -cert client.pem -key client.pem -CAfile server.crt”时它工作正常

最佳答案

许多旧协议(protocol)自 Python v3.6 以来被禁用,namely

这可能是由于服务器和客户端运行在不同的 python 版本(3.6 与 3.7)上。

关于python - 代码适用于 Python 3.6,但不适用于 3.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54297807/

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