I have the following class for opening my websockets:
import os
import asyncio
import websockets
import threading
class Server:
def get_port(self):
return os.getenv('WS_PORT', '9002')
def get_host(self):
return os.getenv('WS_HOST', 'localhost')
def shutdown(self):
self.loop.call_soon_threadsafe(self.loop.stop)
def start(self):
return websockets.serve(self.handler, self.get_host(), self.get_port())
Followed by some async handling methods for incoming messages. The loop function for getting the messages runs in its own thread, just for information.
I am able to terminate the program, however I was not able to close the sockets properly, cause it causes the following error on the restart of the script
OSError: [Errno 10048] error while attempting to bind on address ('127.0.0.1', 9002):
Since the port is already in use. Usually I would use some .close()
function, however the websocket package does not provide such function, so I am wondering if I can get this done with the os package or the websocket package?