我正在尝试使用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:系统lib
我已经检查了权限和路径
最佳答案
在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)
关于python - python warp套接字失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14836881/