
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
simple-websocket-server
Advanced tools
Based on simple-websocket-server.
pip install simple-websocket-server
from simple_websocket_server import WebSocketServer, WebSocket
class SimpleEcho(WebSocket):
def handle(self):
# echo message back to client
self.send_message(self.data)
def connected(self):
print(self.address, 'connected')
def handle_close(self):
print(self.address, 'closed')
server = WebSocketServer('', 8000, SimpleEcho)
server.serve_forever()
Open tests/websocket.html and connect to the server.
from simple_websocket_server import WebSocketServer, WebSocket
class SimpleChat(WebSocket):
def handle(self):
for client in clients:
if client != self:
client.send_message(self.address[0] + u' - ' + self.data)
def connected(self):
print(self.address, 'connected')
for client in clients:
client.send_message(self.address[0] + u' - connected')
clients.append(self)
def handle_close(self):
clients.remove(self)
print(self.address, 'closed')
for client in clients:
client.send_message(self.address[0] + u' - disconnected')
clients = []
server = WebSocketServer('', 8000, SimpleChat)
server.serve_forever()
Open multiple tests/websocket.html and connect to the server.
There is an example which provides a simple echo and chat server
Echo Server
python tests/example_server.py --example echo
Chat Server (open up multiple tests/websocket.html files)
python tests/example_server.py --example chat
Generate a certificate with key
openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout key.pem
Run the secure TLS/SSL server (in this case the cert.pem file is in the same directory)
python tests/example_server.py --example chat --ssl 1
Offer the certificate to the browser by serving tests/websocket.html through https. The HTTPS server will look for cert.pem in the local directory. Ensure the tests/websocket.html is also in the same directory to where the server is run.
python tests/simple_https_server.py
Open a web browser to: https://localhost:443/tests/websocket.html
Change ws://localhost:8000/ to wss://localhost:8000 and click connect.
Note: if you are having problems connecting, ensure that the certificate is added in your browser against the exception https://localhost:8000 or whatever host:port pair you want to connect to.
connected: called when handshake is complete
handle_close: called when the endpoint is closed or there is an error
handle: gets called when there is an incoming message from the client endpoint
send_message: send some text or binary data to the client endpoint
close: send close frame to endpoint
MIT
FAQs
A simple WebSocket server
We found that simple-websocket-server demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.