Quickly create a web service supporting both websockets and http.
Project description
webshoes
Quickly create a web service supporting both websockets and http.
import webshoes as ws
def cmdAdd(ctx, q):
return {'result': int(q.a) + int(q.b)}
wsa = ws.WebShoesApp('127.0.0.1', 15801, {'verbose': True})
wsa.register('cmd', 'cmd', 'args', 'event', 'reply', {
'add': cmdAdd
})
wsa.start()
Table of contents
Install
$ pip3 install webshoes
Examples
# Test function
def cmdAdd(ctx, q):
return {'result': int(q.a) + int(q.b)}
# Create the server
import webshoes as ws
wsa = ws.WebShoesApp('127.0.0.1', 15801, {'verbose': True})
# Register the callback function
# ----------------------------------> The root for http requests
# | ---------------------------> The variable in websocket request
# | | containing the command name
# | | --------------------> The variable in websocket request
# | | | containing the function arguments
# | | | ------------> The variable in websocket request
# | | | | containing the event name
# | | | | ---> Variable in reply to put return data
# | | | | |
wsa.register('cmd', 'cmd', 'args', 'event', 'reply', {
'add': cmdAdd
})
# Start the server
wsa.start()
# Call add function with http request
import requests
reply = requests.get('http://127.0.0.1:15801/cmd/add?a=2&b=3')
Log(reply.text)
# Call the add function with websockets
import websockets
async with websockets.connect('ws://127.0.0.1:15801') as wsc:
await wsc.send(json.dumps({'cmd':'add', 'args':{'a':2, 'b':3}}))
reply = await wsc.recv()
Log(reply)
# Stop the server
wsa.stop()
References
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
webshoes-0.1.5-py3-none-any.whl
(10.0 kB
view details)
File details
Details for the file webshoes-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: webshoes-0.1.5-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2121b3223678bd10938c8d7da54f7de2622b10d1bf7c08d01b239e2bea44f883 |
|
MD5 | bf05e62564ade41aecae45d6aab4e2dd |
|
BLAKE2b-256 | c4e5006a8d305d8f9dba4bf8077541f5dccf5adb80d379bbeb995dab0f193b8c |