Server

Server(WSGI) creates and listens at the HTTP socket, dispatching the requests to a handler.

WSGIRef server but uses threads to handle requests by using the ThreadingMixIn. This is useful to handle web browsers pre-opening sockets, on which Server would wait indefinitely.

can used with any WSGI compatible web framework

this is a wsgiref based server

wsgiref is a built-in WSGI package that provides various classes and helpers to develop against WSGI. Mostly it provides a basic WSGI server that can be used for testing or simple demos. WSocket provides support for websocket on wsgiref for testing purpose. It can only initiate connections one at a time, as a result of being single threaded. but WSocket WSGI server is multi threaded HTTP server. So it can handle many connections at a time.

Run

wsocket.run(
    app=WSocketApp(),
    host="127.0.0.1",
    port=8080,
    handler_cls=FixedHandler, 
    server_cls=ThreadingWSGIServer
)

if app not given it runs demo app you can use following values as host to run local server(named localhost)

  • "localhost"

  • "127.0.0.1"

  • ""

default host is "127.0.0.1".

default port is 8080. If port is 0, It will choose random port.

default handler_cls is FixedHandler(base class of WebsocketHandler)

default server_cls is ThreadingWSGIServer

app should be a valid WSGI application. example :

from wsocket import run, WSocketApp

app = WSocketApp()

run('', 8080, app)

Make Server

wsocket.make_server(
    host,
    port,
    app,
    server_class,
    handler_class
)

Create a new WSGIServer server listening on host and port, accepting connections for app. The return value is an instance of the supplied server_class, and will process requests using the specified handler_class. app must be a WSGI application object, as defined by PEP 3333.

example :

from wsocket import WebSocketHandler, WSocketApp, make_server, ThreadingWSGIServer

server = make_server('', 8080, server_class=ThreadingWSGIServer,
                     handler_class=WebSocketHandler,
                     app=WSocketApp())
server.serve_forever()

ThreadingWSGIServer

wsocket.ThreadingWSGIServer(
    server_address,
    RequestHandlerClass
)

Create a ThreadingWSGIServer instance. server_address should be a (host,port) tuple, and RequestHandlerClass should be the subclass of http.server.BaseHTTPRequestHandler that will be used to process requests.

You do not normally need to call this constructor, as the make_server() function can handle all the details for you.

ThreadingWSGIServer is a subclass of WSGIServer. [ThreadingWSGIServer] also provides these WSGI-specific methods:

set_app(application) - Sets the callable application as the WSGI application that will receive requests.

get_app() - Returns the currently-set application callable.

Normally, however, you do not need to use these additional methods, as [set_app()] is normally called by make_server(), and the [get_app()] exists mainly for the benefit of request handler instances.

Last updated