我正在尝试使用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错误:140B0002:SSL例程:SSL_CTX_use_PrivateKey_file:system lib
我已经检查了权限和路径
答案 0 :(得分:4)
在certfile和keyfile的字符串中,使用"\t",
这是Tab的别名。要么逃避反斜杠("\\"
),请使用os.path.join
或raw strings:
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)