Skip to content

Commit d478e0f

Browse files
authored
Merge pull request Pithikos#123 from MaelkMark/patch-2
Updated WebSocketHandler ssl to work with Python 3.12+
2 parents 56af8ae + fb81db1 commit d478e0f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

websocket_server/websocket_server.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,13 @@ def __init__(self, socket, addr, server):
265265
self._send_lock = threading.Lock()
266266
if server.key and server.cert:
267267
try:
268-
socket = ssl.wrap_socket(socket, server_side=True, certfile=server.cert, keyfile=server.key)
269-
except: # Not sure which exception it throws if the key/cert isn't found
270-
logger.warning("SSL not available (are the paths {} and {} correct for the key and cert?)".format(server.key, server.cert))
268+
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
269+
ssl_context.load_cert_chain(certfile=server.cert, keyfile=server.key)
270+
socket = ssl_context.wrap_socket(socket, server_side=True)
271+
except FileNotFoundError:
272+
logger.warning("SSL key or certificate file not found. Please check the paths for the key and cert.")
273+
except ssl.SSLError as e:
274+
logger.warning(f"SSL error occurred: {e}")
271275
StreamRequestHandler.__init__(self, socket, addr, server)
272276

273277
def setup(self):

0 commit comments

Comments
 (0)