1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
.\" Man page generated from reStructuredText.
.
.
.nr rst2man-indent-level 0
.
.de1 rstReportMargin
\\$1 \\n[an-margin]
level \\n[rst2man-indent-level]
level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
-
\\n[rst2man-indent0]
\\n[rst2man-indent1]
\\n[rst2man-indent2]
..
.de1 INDENT
.\" .rstReportMargin pre:
. RS \\$1
. nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
. nr rst2man-indent-level +1
.\" .rstReportMargin post:
..
.de UNINDENT
. RE
.\" indent \\n[an-margin]
.\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
.nr rst2man-indent-level -1
.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
..
.TH "SIMPLE-WEBSOCKET" "1" "Mar 04, 2025" "" "simple-websocket"
.SH NAME
simple-websocket \- simple-websocket
.sp
Simple WebSocket server and client for Python.
.SH GETTING STARTED
.sp
\fBsimple\-websocket\fP includes a collection of WebSocket servers and clients for
Python, including support for both traditional and asynchronous (asyncio)
workflows. The servers are designed to be integrated into larger web
applications if desired.
.SS Installation
.sp
This package is installed with \fBpip\fP:
.INDENT 0.0
.INDENT 3.5
.sp
.EX
pip install simple\-websocket
.EE
.UNINDENT
.UNINDENT
.SS Server Example #1: Flask
.sp
The following example shows how to add a WebSocket route to a
\X'tty: link https://flask.palletsprojects.com'\fI\%Flask\fP\X'tty: link' application.
.INDENT 0.0
.INDENT 3.5
.sp
.EX
from flask import Flask, request
from simple_websocket import Server, ConnectionClosed
app = Flask(__name__)
@app.route(\(aq/echo\(aq, websocket=True)
def echo():
ws = Server.accept(request.environ)
try:
while True:
data = ws.receive()
ws.send(data)
except ConnectionClosed:
pass
return \(aq\(aq
.EE
.UNINDENT
.UNINDENT
.sp
Integration with web applications using other
\X'tty: link https://wsgi.readthedocs.io'\fI\%WSGI\fP\X'tty: link' frameworks works in a similar way. The
only requirement is to pass the \fBenviron\fP dictionary to the
\fBServer.accept()\fP method to initiate the WebSocket handshake.
.SS Server Example #2: Aiohttp
.sp
The following example shows how to add a WebSocket route to a web application
built with the \X'tty: link https://aiohttp.readthedocs.io'\fI\%aiohttp\fP\X'tty: link' framework.
.INDENT 0.0
.INDENT 3.5
.sp
.EX
from aiohttp import web
from simple_websocket import AioServer, ConnectionClosed
app = web.Application()
async def echo(request):
ws = await AioServer.accept(aiohttp=request)
try:
while True:
data = await ws.receive()
await ws.send(data)
except ConnectionClosed:
pass
return web.Response(text=\(aq\(aq)
app.add_routes([web.get(\(aq/echo\(aq, echo)])
if __name__ == \(aq__main__\(aq:
web.run_app(app, port=5000)
.EE
.UNINDENT
.UNINDENT
.SS Server Example #3: ASGI
.sp
The next server example shows an asynchronous application that supports the
\X'tty: link https://asgi.readthedocs.io'\fI\%ASGI\fP\X'tty: link' protocol.
.INDENT 0.0
.INDENT 3.5
.sp
.EX
from simple_websocket import AioServer, ConnectionClosed
async def echo(scope, receive, send):
ws = await AioServer.accept(asgi=(scope, receive, send))
try:
while True:
data = await ws.receive()
await ws.send(data)
except ConnectionClosed:
pass
.EE
.UNINDENT
.UNINDENT
.SS Client Example #1: Synchronous
.sp
The client example that follows can connect to any of the server examples above
using a synchronous interface.
.INDENT 0.0
.INDENT 3.5
.sp
.EX
from simple_websocket import Client, ConnectionClosed
def main():
ws = Client.connect(\(aqws://localhost:5000/echo\(aq)
try:
while True:
data = input(\(aq> \(aq)
ws.send(data)
data = ws.receive()
print(f\(aq< {data}\(aq)
except (KeyboardInterrupt, EOFError, ConnectionClosed):
ws.close()
if __name__ == \(aq__main__\(aq:
main()
.EE
.UNINDENT
.UNINDENT
.SS Client Example #2: Asynchronous
.sp
The next client uses Python\(aqs \fBasyncio\fP framework.
.INDENT 0.0
.INDENT 3.5
.sp
.EX
import asyncio
from simple_websocket import AioClient, ConnectionClosed
async def main():
ws = await AioClient.connect(\(aqws://localhost:5000/echo\(aq)
try:
while True:
data = input(\(aq> \(aq)
await ws.send(data)
data = await ws.receive()
print(f\(aq< {data}\(aq)
except (KeyboardInterrupt, EOFError, ConnectionClosed):
await ws.close()
if __name__ == \(aq__main__\(aq:
asyncio.run(main())
.EE
.UNINDENT
.UNINDENT
.SH API REFERENCE
.SS The \fBServer\fP class
.SS The \fBAioServer\fP class
.SS The \fBClient\fP class
.SS The \fBAioClient\fP class
.SS Exceptions
.INDENT 0.0
.IP \(bu 2
\fI\%Search Page\fP
.UNINDENT
.SH AUTHOR
Miguel Grinberg
.SH COPYRIGHT
2021, Miguel Grinberg
.\" Generated by docutils manpage writer.
.
|