Package: simple_ws
Simple websocket implementation in pythonInstall$ pip install simple_wsUsageTo test the library, clone repo, open two command windows and cd into
the python-WS directoryRunpython-mhttp.server 8000Runpython ws_example.pyin the other windowOpenhttp://localhost:8000in a browserExamplefromsimple_wsimportWebSocketclassWSHandler(WebSocket):defon_message(self,msg,target_client):forclientinself.clients:ifclient.is_open():client.write_message(msg)defon_open(self,client):print("Client connected!")defon_close(self,client):print("Client left...")defon_ping(self,client):print("Recieved ping!")defon_pong(self,client):print("Recieved pong!")host=''port=8080ws=WSHandler(host,port)WebSocket parametersparametertypedefaultdescriptio
nhostStringHost
domainportIntegerPort
number for
websocketpingBooleanTrueWhether
server
should
ping
client in
a given
intervall,
will close
connection
if pong is
not
receivedping_int ervallInteger5How often
should
server
ping
client in
seconds,
has no
effect if
ping is
set to
falsecompress ionBooleanTrueWhether
messages
should be
compressedmax_fram e_sizeInteger8192Max size
for a
single
websocket
frame. If
payload
exceeds
limit, the
message
will be
split in
several
partsbuffer_s izeInteger4096Max
network
buffer
sizeFunctionsWebSocketon_open(self, client)Called when the server opens a connection to a new client (client).defon_open(self,client):# Executes when opening a connectionon_close(self, client)Called when the server closes a connection to a client (client).defon_close(self,client):# Executes when closing a connectionon_message(self, msg, client)Called when the server has received a message (msg) from a client
(client). The message can be in either binary or text format.defon_message(self,msg,client):# Executes when server recieves a messages from clienton_ping(self, client)Called when the server sends a ping to a client (client).defon_ping(self,client):# Executes when ping is sent to a clienton_pong(self, client)Called when the server receives a pong from a client (client).defon_pong(self,client):# Executes when pong is received from a clientClientwrite_message(self, msg, binary=False)Sends a message payload (msg) to the client. If binary=True, the message
gets sent as binary data.# Text messageclient.write_message("Hello world")# Binary messageclient.write_message(b"0x00",binary=True)is_open(self)Returns True if the connection has gone through handshake, and is
currently open.close(self, status, reason)Sends a close frame to the client, and closes the connection after
either a response, or after 1 second. Status and reason are not
currently implemented. Will ultimately result inWebSocket.on_closebeing fired.client.close(1002,"Pong not recieved")TODOWrite more testsAdd support for payload in ping and pong framesError handlingClean up classesImplement close status and reasonImplement all compression configurationsAdd more configurability/remove hardcoded constantsImplement connection limitExternal sourceshttps://tools.ietf.org/html/rfc6455https://tools.ietf.org/html/rfc7692https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servershttps://www.igvita.com/2013/11/27/configuring-and-optimizing-websocket-compression/https://github.com/tornadoweb/tornadohttps://docs.python.org/3/library/asyncio.htmlLicenseMIT LicenseAuthorsEven Dalen,Audun Wigum
ArboandOle Kristian
Aune