Skip to main content

WebSockets for Flask.

Project description

flaskws3

BIG NOTE: the code is broken for some reason.

Actually good WebSocket for Flask.

Keep in mind that this is just a patched version of this Python 2 original; no changes were made, so original docs are still valid.

For convenience, the module name didn't change too.

Usage

Info is mostly taken from the original docs.

Here's some starting code for this mini tutor:

import flaskws

app = Flask(__name__)
app.wsgi_app = flaskws.WsMiddleware(app.wsgi_app)

Splitted path!

Method #1

For this method, simply create a class for your WebSocket server.

@app.route("/ws/<int:some_id>")
@flaskws.ws_server
class SampleServer():
	def __init__(self, ws_sock, **req_args):
		self.ws_sock = ws_sock
		print(req_args["some_id"])
	def on_open(self, ws_sock): pass
	def on_message(self, ws_sock, frame):
		fin, op, payload = frame # TODO: Search around code to find what info lurks in `frame`.
	def on_close(self, ws_sock): pass

Method #2

This method uses a function. A little harder to control than Method #1, but no one says you can't pick this one!..

@app.route("/ws/<int:some_id>")
@flaskws.ws_server_view
def echo_server(ws_sock, some_id=None):
	while True:
		frame = ws_sock.recv(timeout=5.0)
		if frame:
			fin, op, msg = frame
			if msg:
				ws_sock.send(msg)
				if msg == "close": # Likely a module-implemented way of closing??
					break

Client

As in the unpatched version, there's also a client. The client wasn't changed in any way. Still no SSL support though...

Here's some sample code for you:

from flaskws import ws_connect

with ws_connect("ws://example.com/wsgateway/") as ws: # Could be used as a class too, just remember to `close()`!!
	if ws.handshake():
		ws.send("somethingsomething")
		for frame in c:
			print(frame)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flaskws3-3.0.4.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flaskws3-3.0.4.1-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file flaskws3-3.0.4.1.tar.gz.

File metadata

  • Download URL: flaskws3-3.0.4.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for flaskws3-3.0.4.1.tar.gz
Algorithm Hash digest
SHA256 e39f77404e229d1d2db360ddf472dd6322a6f5e5dacc5db71db24623e1e2632a
MD5 53535df2b7e1192083e697b873c81b74
BLAKE2b-256 f167f1936e5b1a217a6e76dd9c160f55571fc86dee899e7e46ef72bde13942bd

See more details on using hashes here.

File details

Details for the file flaskws3-3.0.4.1-py3-none-any.whl.

File metadata

  • Download URL: flaskws3-3.0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for flaskws3-3.0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba6046fccf9350729a074b4dec64e7ee22bcbc8142ac7516643e098a9f2bf9b4
MD5 2f19638e36f8d33354de4f722a4bf03e
BLAKE2b-256 baa02332cb2199a1eebc93b1b8828975d9f5a66ad0b87e4a536e4b62bd1c1f87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page