Skip to main content

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 hashes)

Uploaded Python 3

Supported by

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