simple-websocket-server


Namesimple-websocket-server JSON
Version 0.4.4 PyPI version JSON
download
home_pagehttps://github.com/pikhovkin/simple-websocket-server
SummaryA simple WebSocket server
upload_time2023-08-05 14:09:45
maintainer
docs_urlNone
authorSergei Pikhovkin
requires_python
licenseMIT
keywords websocket-server websocket websockets websocket-protocol websocket-library
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # A simple WebSocket server

[![GitHub Actions](https://github.com/pikhovkin/simple-websocket-server/workflows/build/badge.svg)](https://github.com/pikhovkin/simple-websocket-server/actions)
[![PyPI](https://img.shields.io/pypi/v/simple-websocket-server.svg)](https://pypi.org/project/simple-websocket-server/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/simple-websocket-server.svg)
[![PyPI - License](https://img.shields.io/pypi/l/simple-websocket-server.svg)](./LICENSE)

Based on [simple-websocket-server](https://github.com/dpallot/simple-websocket-server).

- RFC 6455 (All latest browsers)
- TLS/SSL out of the box
- Passes Autobahns Websocket Testsuite
- Support for Python 2 and 3

#### Installation

    pip install simple-websocket-server

#### Echo Server Example

`````python
from simple_websocket_server import WebSocketServer, WebSocket


class SimpleEcho(WebSocket):
    def handle(self):
        # echo message back to client
        self.send_message(self.data)

    def connected(self):
        print(self.address, 'connected')

    def handle_close(self):
        print(self.address, 'closed')


server = WebSocketServer('', 8000, SimpleEcho)
server.serve_forever()
`````

Open *tests/websocket.html* and connect to the server.

#### Chat Server Example

`````python
from simple_websocket_server import WebSocketServer, WebSocket


class SimpleChat(WebSocket):
    def handle(self):
        for client in clients:
            if client != self:
                client.send_message(self.address[0] + u' - ' + self.data)

    def connected(self):
        print(self.address, 'connected')
        for client in clients:
            client.send_message(self.address[0] + u' - connected')
        clients.append(self)

    def handle_close(self):
        clients.remove(self)
        print(self.address, 'closed')
        for client in clients:
            client.send_message(self.address[0] + u' - disconnected')


clients = []

server = WebSocketServer('', 8000, SimpleChat)
server.serve_forever()
`````
Open multiple *tests/websocket.html* and connect to the server.

#### Want to get up and running faster?

There is an example which provides a simple echo and chat server

Echo Server

    python tests/example_server.py --example echo

Chat Server (open up multiple *tests/websocket.html* files)

    python tests/example_server.py --example chat

#### TLS/SSL Example

1) Generate a certificate with key

        openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout key.pem

2) Run the secure TLS/SSL server (in this case the cert.pem file is in the same directory)

        python tests/example_server.py --example chat --ssl 1

3) Offer the certificate to the browser by serving *tests/websocket.html* through https.
The HTTPS server will look for cert.pem in the local directory.
Ensure the *tests/websocket.html* is also in the same directory to where the server is run.

        python tests/simple_https_server.py

4) Open a web browser to: *https://localhost:443/tests/websocket.html*

5) Change *ws://localhost:8000/* to *wss://localhost:8000* and click connect.

Note: if you are having problems connecting, ensure that the certificate is added in your browser against the exception *https://localhost:8000* or whatever host:port pair you want to connect to.

#### For the Programmers

connected: called when handshake is complete
 - self.address: TCP address port tuple of the endpoint

handle_close: called when the endpoint is closed or there is an error
 - self.address: TCP address port tuple of the endpoint

handle: gets called when there is an incoming message from the client endpoint
 - self.address: TCP address port tuple of the endpoint
 - self.opcode: the WebSocket frame type (STREAM, TEXT, BINARY)
 - self.data: bytearray (BINARY frame) or unicode string payload (TEXT frame)
 - self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)

send_message: send some text or binary data to the client endpoint
 - sending data as a unicode object will send a TEXT frame
 - sending data as a bytearray object will send a BINARY frame

close: send close frame to endpoint

### Licensing

MIT

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/pikhovkin/simple-websocket-server",
    "name": "simple-websocket-server",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "websocket-server,websocket,websockets,websocket-protocol,websocket-library",
    "author": "Sergei Pikhovkin",
    "author_email": "s@pikhovkin.ru",
    "download_url": "https://files.pythonhosted.org/packages/eb/6b/a46e3783168e5e11ec18e88d7650cb242e5a22b8228e9ad8b2375c68afe8/simple-websocket-server-0.4.4.tar.gz",
    "platform": null,
    "description": "# A simple WebSocket server\n\n[![GitHub Actions](https://github.com/pikhovkin/simple-websocket-server/workflows/build/badge.svg)](https://github.com/pikhovkin/simple-websocket-server/actions)\n[![PyPI](https://img.shields.io/pypi/v/simple-websocket-server.svg)](https://pypi.org/project/simple-websocket-server/)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/simple-websocket-server.svg)\n[![PyPI - License](https://img.shields.io/pypi/l/simple-websocket-server.svg)](./LICENSE)\n\nBased on [simple-websocket-server](https://github.com/dpallot/simple-websocket-server).\n\n- RFC 6455 (All latest browsers)\n- TLS/SSL out of the box\n- Passes Autobahns Websocket Testsuite\n- Support for Python 2 and 3\n\n#### Installation\n\n    pip install simple-websocket-server\n\n#### Echo Server Example\n\n`````python\nfrom simple_websocket_server import WebSocketServer, WebSocket\n\n\nclass SimpleEcho(WebSocket):\n    def handle(self):\n        # echo message back to client\n        self.send_message(self.data)\n\n    def connected(self):\n        print(self.address, 'connected')\n\n    def handle_close(self):\n        print(self.address, 'closed')\n\n\nserver = WebSocketServer('', 8000, SimpleEcho)\nserver.serve_forever()\n`````\n\nOpen *tests/websocket.html* and connect to the server.\n\n#### Chat Server Example\n\n`````python\nfrom simple_websocket_server import WebSocketServer, WebSocket\n\n\nclass SimpleChat(WebSocket):\n    def handle(self):\n        for client in clients:\n            if client != self:\n                client.send_message(self.address[0] + u' - ' + self.data)\n\n    def connected(self):\n        print(self.address, 'connected')\n        for client in clients:\n            client.send_message(self.address[0] + u' - connected')\n        clients.append(self)\n\n    def handle_close(self):\n        clients.remove(self)\n        print(self.address, 'closed')\n        for client in clients:\n            client.send_message(self.address[0] + u' - disconnected')\n\n\nclients = []\n\nserver = WebSocketServer('', 8000, SimpleChat)\nserver.serve_forever()\n`````\nOpen multiple *tests/websocket.html* and connect to the server.\n\n#### Want to get up and running faster?\n\nThere is an example which provides a simple echo and chat server\n\nEcho Server\n\n    python tests/example_server.py --example echo\n\nChat Server (open up multiple *tests/websocket.html* files)\n\n    python tests/example_server.py --example chat\n\n#### TLS/SSL Example\n\n1) Generate a certificate with key\n\n        openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout key.pem\n\n2) Run the secure TLS/SSL server (in this case the cert.pem file is in the same directory)\n\n        python tests/example_server.py --example chat --ssl 1\n\n3) Offer the certificate to the browser by serving *tests/websocket.html* through https.\nThe HTTPS server will look for cert.pem in the local directory.\nEnsure the *tests/websocket.html* is also in the same directory to where the server is run.\n\n        python tests/simple_https_server.py\n\n4) Open a web browser to: *https://localhost:443/tests/websocket.html*\n\n5) Change *ws://localhost:8000/* to *wss://localhost:8000* and click connect.\n\nNote: if you are having problems connecting, ensure that the certificate is added in your browser against the exception *https://localhost:8000* or whatever host:port pair you want to connect to.\n\n#### For the Programmers\n\nconnected: called when handshake is complete\n - self.address: TCP address port tuple of the endpoint\n\nhandle_close: called when the endpoint is closed or there is an error\n - self.address: TCP address port tuple of the endpoint\n\nhandle: gets called when there is an incoming message from the client endpoint\n - self.address: TCP address port tuple of the endpoint\n - self.opcode: the WebSocket frame type (STREAM, TEXT, BINARY)\n - self.data: bytearray (BINARY frame) or unicode string payload (TEXT frame)\n - self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)\n\nsend_message: send some text or binary data to the client endpoint\n - sending data as a unicode object will send a TEXT frame\n - sending data as a bytearray object will send a BINARY frame\n\nclose: send close frame to endpoint\n\n### Licensing\n\nMIT\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple WebSocket server",
    "version": "0.4.4",
    "project_urls": {
        "Homepage": "https://github.com/pikhovkin/simple-websocket-server"
    },
    "split_keywords": [
        "websocket-server",
        "websocket",
        "websockets",
        "websocket-protocol",
        "websocket-library"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "68a38ee8c7e6426825476f196fc09bb2d1cac4eafdddd3782ce27768d82c7b1d",
                "md5": "25ce298f44f11a5c96acf02fcd7298a1",
                "sha256": "2d0892a473fe0479f80aed63505d27088a463a420aa085d8c7025e863d0ee3b7"
            },
            "downloads": -1,
            "filename": "simple_websocket_server-0.4.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "25ce298f44f11a5c96acf02fcd7298a1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8930,
            "upload_time": "2023-08-05T14:09:43",
            "upload_time_iso_8601": "2023-08-05T14:09:43.891595Z",
            "url": "https://files.pythonhosted.org/packages/68/a3/8ee8c7e6426825476f196fc09bb2d1cac4eafdddd3782ce27768d82c7b1d/simple_websocket_server-0.4.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eb6ba46e3783168e5e11ec18e88d7650cb242e5a22b8228e9ad8b2375c68afe8",
                "md5": "24077e55c4b458ec294b627123c962f0",
                "sha256": "d083aaa69eefcb6fc87c009d720c0c7d6fe8d95135dc18f855a5688576c33ae6"
            },
            "downloads": -1,
            "filename": "simple-websocket-server-0.4.4.tar.gz",
            "has_sig": false,
            "md5_digest": "24077e55c4b458ec294b627123c962f0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9971,
            "upload_time": "2023-08-05T14:09:45",
            "upload_time_iso_8601": "2023-08-05T14:09:45.572990Z",
            "url": "https://files.pythonhosted.org/packages/eb/6b/a46e3783168e5e11ec18e88d7650cb242e5a22b8228e9ad8b2375c68afe8/simple-websocket-server-0.4.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-05 14:09:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pikhovkin",
    "github_project": "simple-websocket-server",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "simple-websocket-server"
}
        
Elapsed time: 0.13286s