简体   繁体   English

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

[英]Code works on Python 3.6, but does not work in 3.7

I started to learn python and sockets those days and I made a simple client-server app just to make some tests and it works fine with Python 3.6, but gives me error in the newest version.那些天我开始学习 Python 和套接字,我制作了一个简单的客户端 - 服务器应用程序只是为了进行一些测试,它在 Python 3.6 上运行良好,但在最新版本中出现错误。

Server.py服务器.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()

Client.py客户端.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()

Error错误

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)

Edit 1编辑 1

I tried to use ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) and ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT), but I got the same error.我尝试使用 ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) 和 ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT),但我得到了同样的错误。

Edit 2编辑 2

When I use the command "s_client -connect 127.0.0.1:65405 -cert client.pem -key client.pem -CAfile server.crt" in the OpenSSL it works fine当我在 OpenSSL 中使用命令“s_client -connect 127.0.0.1:65405 -cert client.pem -key client.pem -CAfile server.crt”时它工作正常

Many old protocols are disabled since Python v3.6, namely自 Python v3.6 起禁用了许多旧协议,

This might happen due server and client run on different python versions (3.6 vs 3.7).由于服务器和客户端运行在不同的 Python 版本(3.6 与 3.7)上,这可能会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM