我使用websockets来获取 websocket 服务器。这工作得很好。但现在我想通过 wss 使用安全连接。它在这里说you can set the ssl keyword argument to a SSLContext to enable TLS.
,这就是我所做的。
import asyncio
import websockets
import ssl
from subprocess import check_output
import sys
async def onConnect(websocket, path):
while 1:
msg = await websocket.recv()
from subprocess import check_output
out = check_output(["./wsscript.sh", msg])
resp = out.decode("utf-8")
print("< {}".format(resp))
await websocket.send("{}".format(resp))
ctx=ssl.create_default_context()
ctx.load_cert_chain('./cert.pem', './key.pem')
start_server = websockets.serve(onConnect, '10.220.1.2', 8765,ssl=ctx)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
但是我不能再使用 SSLContext 连接到 websocket