enable ssl
This commit is contained in:
parent
02e4569634
commit
2fb661750a
@ -59,10 +59,11 @@ class JsonWebsocketConnection(object):
|
||||
|
||||
|
||||
class JsonWebsocketServer(object):
|
||||
def __init__(self, handler_class: JsonWebsocketConnection, host:str = 'localhost', port:int = 8765):
|
||||
def __init__(self, handler_class: JsonWebsocketConnection, host:str = 'localhost', port:int = 8765, ssl_context = None):
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._handler_class = handler_class
|
||||
self._ssl_context = ssl_context
|
||||
|
||||
def run(self):
|
||||
async def main(websocket: websockets.WebSocketServerProtocol,
|
||||
@ -72,6 +73,9 @@ class JsonWebsocketServer(object):
|
||||
|
||||
await connection.run()
|
||||
|
||||
if (self._ssl_context is not None):
|
||||
start_server = websockets.serve(main, self._host, self._port, ssl=self._ssl_context)
|
||||
else:
|
||||
start_server = websockets.serve(main, self._host, self._port)
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
|
@ -1,7 +1,22 @@
|
||||
import ssl
|
||||
import logging
|
||||
from . import json_websockets
|
||||
from . import crossword_connection
|
||||
|
||||
try:
|
||||
cert_file = "fullchain.pem"
|
||||
key_file = "privkey.pem"
|
||||
|
||||
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
ssl_context.load_cert_chain(cert_file, keyfile=key_file)
|
||||
except Exception as e:
|
||||
logging.error("no ssl context available")
|
||||
ssl_context = None
|
||||
|
||||
|
||||
server = json_websockets.JsonWebsocketServer(
|
||||
crossword_connection.CrosswordConnection
|
||||
crossword_connection.CrosswordConnection, ssl_context=ssl_context
|
||||
)
|
||||
|
||||
|
||||
server.run()
|
Loading…
Reference in New Issue
Block a user