1

我正在尝试使用 ssl 对服务器进行编码,但是有一个我不明白的错误。这是代码:

import socket, ssl, select, sys, exceptions

def do_something(connstream, data):
if not data:
    return False
connstream.write(data)
print data
return True

def deal_with_client(connstream):
data = connstream.read()
# null data means the client is finished with us
while data:
    if not do_something(connstream, data):
        # we'll assume do_something returns False
        # when we're finished with client
        break
    data = connstream.read()
# finished with client

bindsocket = socket.socket()
bindsocket.bind(('127.0.0.1', 240))
bindsocket.listen(5)



while True:
newsocket, fromaddr = bindsocket.accept()
try:
    connstream = ssl.wrap_socket(newsocket, server_side=True, certfile="E:\oromenetwork\test\cert", keyfile="E:\oromenetwork\test\key", ca_certs=None, ssl_version=ssl.PROTOCOL_TLSv1)
    connstream.write("Bonjour")
    deal_with_client(connstream)
except ssl.SSLError as e:
    print "warp error "
    print e
    newsocket.shutdown(socket.SHUT_RDWR)
    newsocket.close()
    exit()
except exceptions.TypeError as e:
    print "other error"
    #print "Unexpected error:", sys.exc_info()[0]
    print e
    newsocket.shutdown(socket.SHUT_RDWR)
    newsocket.close()
    exit()

所以我用 openssl s_client -connect 127.0.0.1:240 测试它

这是错误:[Errno 336265218] _ssl.c:351 error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib

我已经检查了权限和路径

4

1 回答 1

4

在您的证书文件和密钥文件的字符串中,您使用"\t",的是 Tab 的别名。转义反斜杠(如"\\")、使用os.path.join原始字符串

connstream = ssl.wrap_socket(newsocket, server_side=True,
                             certfile=r"E:\oromenetwork\test\cert",
                             #       v^
                             keyfile=r"E:\oromenetwork\test\key",
                             ca_certs=None,ssl_version=ssl.PROTOCOL_TLSv1)
于 2013-02-12T16:20:15.677 回答