Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Python WebSocket Library (RFC 6455)
Python
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
LICENSE
README.md
setup.cfg
setup.py
tox.ini
wslib.py

README.md

py-wslib - Python WebSocket Library


Examples

Client

import time
from wslib import WebSocket, WebSocketHandler

class HelloHandler(WebSocketHandler):

    def onopen(self, protocol):
        print "open"
        self.send('Hello World!')

    def onmessage(self, message):
        print "received message:", message

    def onclose(self):
        print "close"

if __name__ == '__main__':
    ws = WebSocket('ws://127.0.0.1:8080/', HelloHandler())
    ws.connect()
    time.sleep(5)
    ws.close()

Server

from wslib import WebSocketServer, WebSocketHandler

class EchoHandler(WebSocketHandler):

    def onrequest(self, request):
        print "received request"
        request.accept()

    def onopen(self, protocol):
        print "open"

    def onmessage(self, message):
        print "new message:", message
        self.send(message)

    def onclose(self):
        print "closed"

if __name__ == '__main__':
    websocket = WebSocketServer('127.0.0.1', 8080, EchoHandler())
    websocket.serve_forever()
Something went wrong with that request. Please try again.