Git Product home page Git Product logo

simplewebsocketserver's Introduction

A very simple WebSocket Server written in Python

No package installation, just one file, enjoy

Supports

  • Hixie 76 (Safari and iPhone)
  • RFC 6455 (All latest browsers)
  • TLS/SSL

A Simple Echo Server Example

  1. Write the client code by extending WebSocket
    from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
    
    class SimpleEcho(WebSocket):
        
        def handleMessage(self):
            if self.data is None:
                self.data = ''
                
            # echo message back to client
            self.sendMessage(str(self.data))
        
        def handleConnected(self):
            print self.address, 'connected'
              
        def handleClose(self):
            print self.address, 'closed'

    server = SimpleWebSocketServer('', 8000, SimpleEcho)
    server.serveforever()
  1. Run your code

  2. Open up websocket.html and connect to the server

Want to get up and running faster?

There is an example which provides a simple echo and chat server

Echo Server

python SimpleExampleServer.py --example echo

Chat Server (open up multiple websocket.html files)

python SimpleExampleServer.py --example chat

TLS/SSL Example

  1. Generate a certificate with key

    openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem

  2. Run the secure TSL/SSL server (in this case the cert.pem file is in the same directory)

    python SimpleExampleServer.py --example chat --ssl 1 --cert ./cert.pem

  3. Offer the certificate to the browser by serving websocket.html through https. The HTTPS server will look for cert.pem in the local directory. Ensure the websocket.html is also in the same directory to where the server is run.

    sudo python SimpleHTTPSServer.py

  4. Open a web browser to: https://localhost:443/websocket.html

  5. 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.

For the Programmers

def handleConnected(): called when handskake is complete

def handleClose(): called when the endpoint is closed or there is an error

def handleMessage(): gets called when there is an incoming message from the client endpoint

  • self.opcode: the WebSocket frame type (STREAM, TEXT, BINARY)
  • self.data: bytearray payload or None if there was no payload
  • self.address: TCP address port tuple of the endpoint
  • self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)
  • self.server.connections: map containing all the clients connected to the server

def sendMessage(buffer): send some text or binary data to the client endpoint

  • sending a buffer as str() will send a text based WebSocket frame otherwise a binary frame

def sendClose() : send close frame to endpoint

simplewebsocketserver's People

Contributors

dpallot avatar

Watchers

Brent Hoover avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.