├── docs
├── _config.yml
├── websocket.md
├── middleware.md
├── app.md
├── README.md
├── server.md
├── handler.md
└── framework.md
├── assests
└── icon.png
├── setup.cfg
├── MANIFEST.in
├── .github
├── labeler.yml
└── ISSUE_TEMPLATE
│ ├── feature_request.md
│ └── bug_report.md
├── pyproject.toml
├── test.py
├── LICENSE
├── client.html
├── tox.ini
├── .gitignore
├── README.md
├── setup.py
└── wsocket.py
/docs/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-cayman
--------------------------------------------------------------------------------
/assests/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ksenginew/WSocket/HEAD/assests/icon.png
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [bdist_wheel]
2 | universal = 1
3 |
4 | [metadata]
5 | license_file = LICENSE
6 |
--------------------------------------------------------------------------------
/MANIFEST.in:
--------------------------------------------------------------------------------
1 | include wsocket.py
2 | include setup.py
3 | include README.rst
4 | include LICENSE
5 | include test/views/*.tpl
6 | include test/*.py
7 |
--------------------------------------------------------------------------------
/.github/labeler.yml:
--------------------------------------------------------------------------------
1 | # Add 'repo' label to any root file changes
2 | repo:
3 | - ./*
4 |
5 | docs:
6 | - ./docs/*
7 |
8 | source:
9 | - ./*.py
10 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [build-system]
2 | # These are the assumed default build requirements from pip:
3 | # https://pip.pypa.io/en/stable/reference/pip/#pep-517-and-518-support
4 | requires = ["setuptools>=40.8.0", "wheel"]
5 | build-backend = "setuptools.build_meta"
6 |
--------------------------------------------------------------------------------
/docs/websocket.md:
--------------------------------------------------------------------------------
1 |
2 | # Websocket
3 | ### Class variables
4 | - `origin` - HTTP `Origin` header
5 |
6 | - `protocol` - supported websocket sub protocol
7 |
8 | - `version` - websocket version(1, 8 or 7)
9 |
10 | - `path` - required path by client(eg:-if websocket url which client opened is `ws://localhost/hello/world?user=Ksengine&pass=1234`, path is `/hello/world`)
11 |
12 | - `logger` = default logger([Python Docs](https://docs.python.org/3/library/logging.html))
13 |
14 | - `do_compress` - is compressed messages required by client
15 |
16 | ### Class methods
17 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: FEATURE
5 | labels: enhancement, feature
6 | assignees: Ksenginew
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: BUG
5 | labels: bug
6 | assignees: Ksenginew
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Additional context**
27 | Add any other context about the problem here.
28 |
29 | ---
30 |
31 | **Practicalities**
32 | - YES/NO I am willing to work on this issue myself.
33 |
--------------------------------------------------------------------------------
/test.py:
--------------------------------------------------------------------------------
1 | from bottle import request, Bottle
2 | from wsocket import WebSocketHandler,logger
3 | from sl.server import ThreadingWSGIServer
4 | from time import sleep
5 | logger.setLevel(10)
6 | app = Bottle()
7 |
8 | @app.route('/')
9 | def handle_websocket():
10 | wsock = request.environ.get('wsgi.websocket')
11 | if not wsock:
12 | return 'Hello World!'
13 | while True:
14 | message = wsock.receive()
15 | if not message:
16 | break
17 | print(message)
18 | wsock.send('Your message was: %r' % message)
19 | sleep(3)
20 | wsock.send('Your message was: %r' % message)
21 |
22 | httpd = ThreadingWSGIServer(('localhost',9001),WebSocketHandler)
23 | httpd.set_app(app)
24 | print('WSGIServer: Serving HTTP on port 9001 ...\n')
25 | try:
26 | httpd.serve_forever()
27 | except:
28 | print('WSGIServer: Server Stopped')
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Ksengine
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/client.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Simple client
4 |
5 |
56 |
57 |
58 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/tox.ini:
--------------------------------------------------------------------------------
1 | # this file is *not* meant to cover or endorse the use of tox or pytest or
2 | # testing in general,
3 | #
4 | # It's meant to show the use of:
5 | #
6 | # - check-manifest
7 | # confirm items checked into vcs are in your sdist
8 | # - python setup.py check
9 | # confirm required package meta-data in setup.py
10 | # - readme_renderer (when using a ReStructuredText README)
11 | # confirms your long_description will render correctly on PyPI.
12 | #
13 | # and also to help confirm pull requests to this project.
14 |
15 | [tox]
16 | envlist = py{35,36,37,38}
17 |
18 | # Define the minimal tox version required to run;
19 | # if the host tox is less than this the tool with create an environment and
20 | # provision it with a tox that satisfies it under provision_tox_env.
21 | # At least this version is needed for PEP 517/518 support.
22 | minversion = 3.3.0
23 |
24 | # Activate isolated build environment. tox will use a virtual environment
25 | # to build a source distribution from the source tree. For build tools and
26 | # arguments use the pyproject.toml file as specified in PEP-517 and PEP-518.
27 | isolated_build = true
28 |
29 | [testenv]
30 | deps =
31 | check-manifest >= 0.42
32 | # If your project uses README.rst, uncomment the following:
33 | # readme_renderer
34 | flake8
35 | pytest
36 | commands =
37 | check-manifest --ignore 'tox.ini,tests/**'
38 | # This repository uses a Markdown long_description, so the -r flag to
39 | # `setup.py check` is not needed. If your project contains a README.rst,
40 | # use `python setup.py check -m -r -s` instead.
41 | python setup.py check -m -s
42 | flake8 .
43 | py.test tests {posargs}
44 |
45 | [flake8]
46 | exclude = .tox,*.egg,build,data
47 | select = E,W,F
48 |
--------------------------------------------------------------------------------
/docs/middleware.md:
--------------------------------------------------------------------------------
1 | ## Middleware
2 |
3 | convert any WSGI compatible web framework to Websocket+HTTP framework using middleware. **works with many WSGI compatible servers**
4 |
5 | **can used with any [WSGI](http://www.wsgi.org/) compatible web framework**
6 | > Flask, Django, Pyramid, Bottle, ... supported
7 |
8 | This middleware adds [`wsgi.websocket`](https://github.com/Ksengine/WSocket/tree/master/docs/websocket.md) variable to [WSGI](http://www.wsgi.org/) environment dictionary.
9 |
10 | example with [bottle](https://github.com/bottlepy/bottle):
11 | ```python
12 | from bottle import request, Bottle
13 | from wsocket import WSocketApp, WebSocketError, logger, run
14 | from time import sleep
15 |
16 | logger.setLevel(10) # for debugging
17 |
18 | bottle = Bottle()
19 | app = WSocketApp(bottle)
20 | # app = WSocketApp(bottle, "WAMP")
21 |
22 | @bottle.route("/")
23 | def handle_websocket():
24 | wsock = request.environ.get("wsgi.websocket")
25 | if not wsock:
26 | return "Hello World!"
27 |
28 | while True:
29 | try:
30 | message = wsock.receive()
31 | if message != None:
32 | print("participator : " + message)
33 | wsock.send("you : "+message)
34 | sleep(2)
35 | wsock.send("you : "+message)
36 | except WebSocketError:
37 | break
38 | run(app)
39 | ```
40 | > you should use HTTP version `1.1` Server with your [WSGI](http://www.wsgi.org/) framework for some clients like Firefox browser
41 |
42 | **for examples on other web frameworks visit [`examples/frameworks`](https://github.com/Ksengine/WSocket/tree/master/examples/frameworks) folder
43 | ## `class WSocketApp(app=None, protocol=None)`
44 | `app` should be a valid [WSGI](http://www.wsgi.org/) web application.
45 | `protocol` is websocket sub protocol to accept (ex: [WAMP](https://wamp-proto.org/))
46 |
47 | ### Class variables
48 |
49 | `GUID` - unique ID to generate websocket accept key
50 |
51 | `SUPPORTED_VERSIONS` - 13, 8 or 7
52 |
53 | `websocket_class` - `"wsgi.websocket"` in WSGI Environ
54 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
--------------------------------------------------------------------------------
/docs/app.md:
--------------------------------------------------------------------------------
1 | ## App
2 |
3 | Event based app for websocket communication. This is app that uses [Framework](framework.md). If not events handled by developer. this app works like demo(echo) app.
4 | This is a [WSGI](http://www.wsgi.org/) web app. So you can use any [WSGI](http://www.wsgi.org/) Server to host this app
5 | > you should use HTTP version `1.1` Server with your [WSGI](http://www.wsgi.org/) framework for some clients like Firefox browser
6 |
7 | ```python
8 | from wsocket import WSocketApp, WebSocketError, logger, run
9 | from time import sleep
10 |
11 | logger.setLevel(10) # for debugging
12 |
13 | def on_close(self, message, client):
14 | print(repr(client) + " : " + message)
15 |
16 | def on_connect(client):
17 | print(repr(client) + " connected")
18 |
19 | def on_message(message, client):
20 | print(repr(clent) + " : " + repr(message))
21 | try:
22 | client.send("you said: " + message)
23 | sleep(2)
24 | client.send("you said: " + message)
25 |
26 | except WebSocketError:
27 | pass
28 |
29 | app = WSocketApp()
30 | app.onconnect += on_connect
31 | app.onmessage += on_message
32 | app.onclose += on_close
33 |
34 | run(app)
35 | ```
36 | > for more info on `client` see - https://github.com/Ksengine/WSocket/tree/master/docs/websocket.md
37 |
38 |
39 | ## `class WSocketApp(app=None, protocol=None)`
40 | `app` should be a valid [WSGI](http://www.wsgi.org/) web application.
41 | `protocol` is websocket sub protocol to accept (ex: [WAMP](https://wamp-proto.org/))
42 |
43 | ### Class variables
44 |
45 | `GUID` - unique ID to generate websocket accept key
46 |
47 | `SUPPORTED_VERSIONS` - 13, 8 or 7
48 |
49 | `websocket_class` - `"wsgi.websocket"` in WSGI Environ
50 |
51 | ### Events
52 |
53 | `onconnect` - fires when client sent a message
54 | `onmessage` - fires when client sent a message
55 | `onmessage` - fires when client sent a message
56 |
57 | you can attach event handler method to event using
58 | - `+=` operator
59 | ```python
60 | app = WSocketApp()
61 | app.onmessage += on_message
62 | app.onmessage += on_message2
63 | # both `on_message` and `on_message2` can handle event
64 | run(app)
65 | ```
66 |
67 | - `+` operator
68 | ```python
69 | app = WSocketApp()
70 | app.onmessage + on_message
71 | app.onmessage + on_message2
72 | # both `on_message` and `on_message2` can handle event
73 | run(app)
74 | ```
75 |
76 | - `=` operator
77 | ```python
78 | app = WSocketApp()
79 | app.onmessage = on_message
80 | # only `on_message` can handle event
81 | app.onmessage = on_message2
82 | # now, only `on_message2` can handle event
83 | run(app)
84 | ```
85 | > You can't add new handlers to Event after `=` operator used. It replaces Event. But you can replace it again using another handler.
86 |
--------------------------------------------------------------------------------
/docs/README.md:
--------------------------------------------------------------------------------
1 | # WSocket
2 | **Simple WSGI HTTP + Websocket Server, Framework, Middleware And App.**
3 |
4 | [](https://pepy.tech/project/wsocket)
5 |
6 | **Note:**
7 | I am a 16 years old student.I have no enough knowledge. So can anyone [help me](https://github.com/Ksengine/WSocket/issues/2) to develop this library?
8 | ## Server
9 | Server([WSGI](http://www.wsgi.org/)) creates and listens at the HTTPsocket, 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.
10 | **can used with any WSGI compatible web framework**
11 |
12 | ## Middleware
13 | convert any WSGI compatible web framework to Websocket+HTTP framework
14 | using middleware.
15 | **works with many WSGI compatible servers**
16 | **can used with any WSGI compatible web framework**
17 | > Flask, Django, Pyramid, Bottle, ... supported
18 |
19 | ## Handler
20 | `wsgiref.simple_server.WSGIRequestHandler` like class named `FixedHandler` that always wrap WSGI app using Middleware.
21 | changes from `WSGIRequestHandler` :
22 | - Prevents reverse DNS lookups
23 | - errorless logger
24 | - use `ServerHandler` to make it WSGI
25 |
26 | > You can convert wsgiref to a websocket+HTTP server using this handler
27 |
28 | ### ServerHandler
29 | `wsgiref.simple_server.ServerHandler`(inherited from `wsgiref.handlers.ServerHandler` like handler named `FixedServerHandler` .
30 | changes from `ServerHandler` :
31 | - set HTTP version to `1.1` because versions below `1.1` are not supported some clients like Firefox.
32 | - removed hop-by-hop headers checker because it raise errors on `Upgrade` and `Connection` headers
33 | - check that all headers are strings
34 |
35 | ## Framework
36 | basic WSGI web application framework that uses Middleware.
37 | - simple routes handler
38 | - auto description to status code
39 | - headers checker
40 | - send data as soon as possible
41 | - send strings, bytes, lists(even bytes and strings mixed) or files directly
42 | - error catcher and error logger
43 |
44 | **works with many WSGI compatible servers**
45 |
46 | ## App
47 | Event based app for websocket communication. this is app that uses Framework
48 | if not events handled by developer. this app works like demo(echo) app.
49 |
50 | ## Features
51 | all Middleware, Handler, Framework and App has following features.
52 | - websocket sub protocol supported
53 | - websocket message compression supported (works if client asks)
54 | - receive and send pong and ping messages(with automatic pong sender)
55 | - receive and send binary or text messages
56 | - works for messages with or without mask
57 | - closing messages supported
58 | - auto and manual close
59 |
60 | **View Documentaion** - https://wsocket.gitbook.io/
61 | **Report Bugs** - https://github.com/Ksengine/WSocket/issues/new/
62 |
63 | ### License
64 | Code and documentation are available according to the MIT License (see [LICENSE](https://github.com/Ksengine/WSocket/blob/master/LICENSE)).
65 |
--------------------------------------------------------------------------------
/docs/server.md:
--------------------------------------------------------------------------------
1 | # Server
2 | Server([WSGI](http://www.wsgi.org/)) 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**
3 | > this is a wsgiref based server
4 |
5 | [`wsgiref`](https://docs.python.org/3/library/wsgiref.html "(in Python v3.x)") 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.
6 | **but WSocket WSGI server is multi threaded HTTP server. So it can handle many connections at a time.**
7 |
8 | ## `wsocket.run(app=WSocketApp(), host="127.0.0.1", port=8080, handler_cls=FixedHandler, server_cls=ThreadingWSGIServer)`
9 | if app not given it runs demo app
10 | you can use following values as `host` to run local server(named localhost)
11 | - `"localhost"`
12 | - `"127.0.0.1"`
13 | - `""`
14 |
15 | default `host` is "127.0.0.1".
16 | default `port` is 8080. If host is 0, It will choose random port
17 | default `handler_cls` is [`FixedHandler`](handler.md)
18 | default `server_cls` is [`ThreadingWSGIServer`](#`wsocket.ThreadingWSGIServer`)
19 | `app` should be a valid [WSGI](http://www.wsgi.org/) application.
20 | **example :**
21 | ```python
22 | from wsocket import run, WSocketApp
23 | app = WSocketApp()
24 | run('', 8080, app)
25 | ```
26 |
27 | ## `wsocket.make_server(host, port, app, server_class, handler_class)`
28 | 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).
29 |
30 | **example :**
31 | ```python
32 | from wsocket import WebSocketHandler, WSocketApp, make_server, ThreadingWSGIServer
33 |
34 | server = make_server('', 8080, server_class=ThreadingWSGIServer,
35 | handler_class=WebSocketHandler,
36 | app=WSocketApp())
37 | server.serve_forever()
38 | ```
39 |
40 | ## `wsocket.ThreadingWSGIServer(server_address, RequestHandlerClass)`
41 | Create a `ThreadingWSGIServer` instance. _server_address_ should be a `(host,port)` tuple, and _RequestHandlerClass_ should be the subclass of [`http.server.BaseHTTPRequestHandler`](https://docs.python.org/3/library/http.server.html#http.server.BaseHTTPRequestHandler "http.server.BaseHTTPRequestHandler") that will be used to process requests.
42 |
43 | You do not normally need to call this constructor, as the [`make_server()`](#make_server) function can handle all the details for you.
44 |
45 | `ThreadingWSGIServer` is a subclass of [`WSGIServer`](https://docs.python.org/3/library/wsgiref.html#wsgiref.simple_server.WSGIServer "wsgiref.simple_server.WSGIServer"). [`ThreadingWSGIServer`] also provides these WSGI-specific methods:
46 |
47 | `set_app(application)` - Sets the callable _application_ as the WSGI application that will receive requests.
48 |
49 | `get_app()` - Returns the currently-set application callable.
50 |
51 | Normally, however, you do not need to use these additional methods, as [`set_app()`] is normally called by [`make_server()`](#make_server), and the [`get_app()`] exists mainly for the benefit of request handler instances.
52 |
--------------------------------------------------------------------------------
/docs/handler.md:
--------------------------------------------------------------------------------
1 |
2 | # Server
3 | Server([WSGI](http://www.wsgi.org/)) 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**
4 | > this is a wsgiref based server
5 |
6 | [`wsgiref`](https://docs.python.org/3/library/wsgiref.html "(in Python v3.x)") 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.
7 | **but WSocket WSGI server is multi threaded HTTP server. So it can handle many connections at a time.**
8 |
9 | ## `wsocket.run(app=WSocketApp(), host="127.0.0.1", port=8080, handler_cls=FixedHandler, server_cls=ThreadingWSGIServer)`
10 | if app not given it runs demo app
11 | you can use following values as `host` to run local server(named localhost)
12 | - `"localhost"`
13 | - `"127.0.0.1"`
14 | - `""`
15 |
16 | default `host` is "127.0.0.1".
17 | default `port` is 8080. If host is 0, It will choose random port
18 | default `handler_cls` is [`FixedHandler`](handler.md)
19 | default `server_cls` is [`ThreadingWSGIServer`](#`wsocket.ThreadingWSGIServer`)
20 | `app` should be a valid [WSGI](http://www.wsgi.org/) application.
21 | **example :**
22 | ```python
23 | from wsocket import run, WSocketApp
24 | app = WSocketApp()
25 | run('', 8080, app)
26 | ```
27 |
28 | ## `wsocket.make_server(host, port, app, server_class, handler_class)`
29 | 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).
30 |
31 | **example :**
32 | ```python
33 | from wsocket import WebSocketHandler, WSocketApp, make_server, ThreadingWSGIServer
34 |
35 | server = make_server('', 8080, server_class=ThreadingWSGIServer,
36 | handler_class=WebSocketHandler,
37 | app=WSocketApp())
38 | server.serve_forever()
39 | ```
40 |
41 | ## `wsocket.ThreadingWSGIServer(server_address, RequestHandlerClass)`
42 | Create a `ThreadingWSGIServer` instance. _server_address_ should be a `(host,port)` tuple, and _RequestHandlerClass_ should be the subclass of [`http.server.BaseHTTPRequestHandler`](https://docs.python.org/3/library/http.server.html#http.server.BaseHTTPRequestHandler "http.server.BaseHTTPRequestHandler") that will be used to process requests.
43 |
44 | You do not normally need to call this constructor, as the [`make_server()`](#make_server) function can handle all the details for you.
45 |
46 | `ThreadingWSGIServer` is a subclass of [`WSGIServer`](https://docs.python.org/3/library/wsgiref.html#wsgiref.simple_server.WSGIServer "wsgiref.simple_server.WSGIServer"). [`ThreadingWSGIServer`] also provides these WSGI-specific methods:
47 |
48 | `set_app(application)` - Sets the callable _application_ as the WSGI application that will receive requests.
49 |
50 | `get_app()` - Returns the currently-set application callable.
51 |
52 | Normally, however, you do not need to use these additional methods, as [`set_app()`] is normally called by [`make_server()`](#make_server), and the [`get_app()`] exists mainly for the benefit of request handler instances.
53 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://pepy.tech/project/wsocket)
2 | [](https://github.com/Ksengine/WSocket/issues)
3 | [](https://github.com/Ksengine/WSocket/network)
4 | [](https://github.com/Ksengine/WSocket/stargazers)
5 | [](https://github.com/Ksengine/WSocket/blob/master/LICENSE)
6 | [](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2FKsengine%2FWSocket)
7 |
8 |
9 |