Framework

basic WSGI web application framework that uses Middleware.

Features

  • simple routes handler

  • auto description to status code

  • headers checker

  • send data as soon as possible

  • send strings, bytes, lists(even bytes and strings mixed) or files directly

  • error catcher and error logger

works with many WSGI compatible servers

you should use HTTP version 1.1 Server with your WSGI framework for some clients like Firefox browser

from wsocket import WSocketApp, WebSocketError, logger, run
from time import sleep

logger.setLevel(10)  # for debugging

app = WSocketApp()
# app = WSocketApp(protocol="WAMP")

@app.route("/")
def handle_websocket(environ, start_response):

    wsock = environ.get("wsgi.websocket")

    if not wsock:
        start_response()
        return "Hello World!"

    while True:
        try:
            message = wsock.receive()
            if message != None:
                print("participator : " + message)
                wsock.send("you : "+message)
                sleep(2)
                wsock.send("you : "+message)
                
        except WebSocketError:
            break
            
run(app)

see Websocket for more info on wsgi.websocket variable in WSGI environment dictionary.

Websocket

WSocketApp

class WSocketApp(app=None, protocol=None)

Class variables

GUID - unique ID to generate websocket accept key

SUPPORTED_VERSIONS - 13, 8 or 7

routes - dictionary for route handlers

websocket_class - "wsgi.websocket" in WSGI Environ

Methods

not_found(self, environ, start_response) - handle 404 NOT FOUND error

route(self, r) - register routes

Routes

WSocket uses simple routes engine. How it works?

  • URL - http://localhost:8080/hello/world?user=Ksengine&pass=1234

  • divided into parts(by Server)

    origin

    host

    port

    path

    query

    http

    localhost

    8080

    /hello/world

    user=Ksengine&pass=1234

    only path is used to find routes

  • walk through routes dictionary

    • if "/hello/world" path found, trigger route handler

      • else, if some route string ends with "*" and path starts with that string trigger route handler

Status and Headers

call start_response to send status code and headers. if you returns without calling it. It will send 200 OK status and some basic headers to client. if start_response is called without argsuments, it will send 200 OK status and some basic headers to client.

start_response has two arguments

  • status - status code as int(eg:-200) or str(eg:- "200 OK" or "200"). If status description(eg:-"OK") not supplied, it will find it.

  • headers - HTTP headers. can passed as,

    • list of tuples

    • dictionary

status code examples:-

send headers examples:-

  • list of tuples

  • dictionary

Send data

You can send following data types

  • str - string, text

  • bytes - binary

  • files - opened files

  • file-like object - streams(text, binary) like StringIOor BytesIO

  • iterables - list, tuple, set dict generators etc.

    generators can send data one by one with time intervals. so it's like async Server

  • other -

    Errors

    example :-

    Internal Server Error(500)

    ZeroDivisionError :division by zero

    report

report button starts reporting issue and logger will print error to python console

Last updated

Was this helpful?