gevent-ws
A fast, MIT-licensed alternative to the abandoned non-free (as in freedom) gevent-websocket.
Why?
It's MIT-licensed, so it can be included to almost all software (both open-source and closed-source) without any licensing issues, as opposed to gevent-websocket. And it's several times faster!
Installation
gevent-ws can be installed from PyPI and GitHub.
PyPI
$ pip3 install gevent-ws
GitHub
$ git clone https://github.com/imachug/gevent-ws.git
$ cd gevent-ws
$ python3 setup.py install
Example
A simple echo server
from gevent.pywsgi import WSGIServer
from gevent_ws import WebSocketHandler
def app(env, start_response):
ws = env["wsgi.websocket"]
while True:
msg = ws.receive()
if msg is None:
break
ws.send(msg)
return [b"Bye"]
server = WSGIServer(("", 8080), app, handler_class=WebSocketHandler)
server.serve_forever()
Both HTTP and websocket on a single port
from gevent.pywsgi import WSGIServer
from gevent_ws import WebSocketHandler
def app(env, start_response):
if "wsgi.websocket" in env:
ws = env["wsgi.websocket"]
while True:
msg = ws.receive()
if msg is None:
break
ws.send(msg)
return [b"Bye"]
else:
start_response("200 OK", [
("Content-Type", "text/html, charset=utf-8")
])
return [
b"""
<script>
const ws = new WebSocket("ws://127.0.0.1:8080/");
ws.onopen = () => {
ws.send("Hello, world!");
};
ws.onmessage = e => {
ws.send(e.data);
};
</script>
"""
]
server = WSGIServer(("", 8080), app, handler_class=WebSocketHandler)
server.serve_forever()
Docs
gevent_ws.WebSocketHandler
The main class. Pass it to gevent.pywsgi.WSGIServer as handler_class.
gevent_ws.WebSocket
You get this object by accessing wsgi.websocket property of the environment object. If it exists, it's a websocket connection; if it doesn't, it's an HTTP request. You can use "wsgi.websocket" in env to check this and route the request correctly.
gevent_ws.WebSocket.receive()
Waits for a message and returns it, either as str or bytes. Blocks. May return None if the connection is closed or an OSError if the socket can't be read from.
gevent_ws.WebSocket.receive_nowait()
Returns the message if it's available (the format is the same as in receive()) or None if no message is in the queue. May return None or raise OSError (see receive()).
gevent_ws.WebSocket.send(message)
Sends a message to the other party. message can be either str or bytes. May raise EOFError if the connection is closed or OSError if the message can't be sent for some other reason.
gevent_ws.WebSocket.close(status=1000)
Closes the websocket. Status 1000 (the default one) means that the connection is closed cleanly. Check RFC for more status codes.
gevent_ws.WebSocket.set_max_message_length(length)
Set maximum incoming message size (in bytes). 10 MiB is the default value.
gevent_ws.WebSocket.closed
A boolean showing whether the connection is closed (by any of the two parties) or is being closed.
gevent_ws.WebSocket.status
The close status code sent by the other party. None if the socket was closed by the client, the server didn't return the error code or the connection was aborted abnormally.
Metadata
Release files for gevent-ws 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| gevent-ws-2.1.0.tar.gz | 5.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gevent_ws-2.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.3 kB
Release files / gevent-ws-2.1.0.tar.gz
| Download URL | gevent-ws-2.1.0.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f69a80dac1c6f353ba338f2570a19d3076be94b49f348a570effd0ab9ffed4a4
|
|
BLAKE2b-256 checksum How to use checksums |
04328797ddd58c8f87d2ff07f2198c1b70dd12f2fdf81b95738cc14873537731
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
|
Release files / gevent_ws-2.1.0-py3-none-any.whl
| Download URL | gevent_ws-2.1.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dba0ea1dc87319385728ba32009af7324144dcbaac1139987830b556914ea2a6
|
|
BLAKE2b-256 checksum How to use checksums |
f54fa80e131f0d2039a5effce6edd9a6c9365f89dc5b24d64b84666f81fa17dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
|
Security for gevent-ws 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.