> For the complete documentation index, see [llms.txt](https://wsocket.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wsocket.gitbook.io/docs/guide/server.md).

# Server

WSGIRef server but uses threads to handle requests by using the [ThreadingMixIn](https://docs.python.org/3/library/socketserver.html#socketserver.ThreadingMixIn). This is useful to handle web browsers pre-opening sockets, on which Server would wait indefinitely.&#x20;

**can used with any WSGI compatible web framework**

> this is a wsgiref based server

[`wsgiref`](https://docs.python.org/3/library/wsgiref.html) 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

```python
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".

&#x20;default `port` is 8080. If port is 0, It will choose random port.

default `handler_cls` is [`FixedHandler`](https://app.gitbook.com/s/-MLY2lyYNPPcwUUNQwto/guide/handler.md)(base class of [WebsocketHandler](/docs/master.md))

default `server_cls` is [`ThreadingWSGIServer`](https://wsocket.gitbook.io/docs/guide/pages/-MMGhfAdqXovrNkU6Z9t#%60wsocket.ThreadingWSGIServer%60)&#x20;

`app` should be a valid [WSGI](http://www.wsgi.org/) application. **example :**

```python
from wsocket import run, WSocketApp

app = WSocketApp()

run('', 8080, app)
```

## Make Server

```python
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**](https://www.python.org/dev/peps/pep-3333).

**example :**

```python
from wsocket import WebSocketHandler, WSocketApp, make_server, ThreadingWSGIServer

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

## ThreadingWSGIServer

```python
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()`](broken://pages/-MMGhfAdqXovrNkU6Z9t#make_server) function can handle all the details for you.

`ThreadingWSGIServer` is a subclass of [`WSGIServer`](https://docs.python.org/3/library/wsgiref.html#wsgiref.simple_server.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()`](broken://pages/-MMGhfAdqXovrNkU6Z9t#make_server), and the \[`get_app()`] exists mainly for the benefit of request handler instances.
