Skip to main content

Python switchboard utilites

Project description

Switchboard Python provides helpers for writing Switchboard workers and clients in Python.

Installation

It’s simplest to install this library from [PyPi](https://pypi.python.org/pypi):

pip install switchboard-python

To build from source:

# Building ./setup.py build

# Running the tests ./setup.py test

# Development install pip install -e .

# Actual install pip install .

Usage

The switchboard.Client class is used to interact with both Switchboard workers and clients.

Assuming that the Switchboard application is running, the following example opens a connection to the server over the worker interface, and sends a batch request with a connect command (see the interfaces guide for command documentation).

worker = switchboard.Client("ws://192.168.50.2:8080/workers")
worker.connect()
worker.send_cmds(("connect", CONN_SPEC))
worker.run_forever()

To handle command responses, send_cmds returns a promise that is fulfilled by the tuple (cmds, resps) when the command’s responses arrive, where cmds is the list of commands given to send_cmds, and resps is the list of responses returned by Switchboard.

def handle_get_mailboxes((cmds, resps)):
    print "For cmds", cmds, ", received resps:", resps

worker.send_cmds(("getMailboxes", {}).then(handle_get_mailboxes)

To add commands on connect, and/or handling of unsolicited messages subclass the base switchboard.Client – an unsolicited message is not sent in response to a command, but when the server has new information, such as a new emails arriving

class TheWorker(switchboard.Client):
    def opened(self):
        print "Connected to Switchboard, issuing watchAll cmd."
        worker.send_cmds(("watchAll", {}))

    def received_unsolicited(resps):
        print "Received unsolicited resps from server:", resps
        for resp in resps:
            if resp[0] == 'newMessage':
                print "New message:", resp[1]


worker = TheWorker("ws://127.0.0.1:8080/workers")
worker.connect()
worker.run_forever()

Examples

All examples are located under /examples. Each example uses command line arguments which you can investigate via:

./examples/example.py --help

listener.py

This worker provides a simple example of bidirectional communication using the Switchboard worker interface. It listens for Switchboard to notify it of new emails, then fetches the raw email and parses it using the Python email module:

./examples/listener.py

apnsworker.py

This worker sends new email Apple Push Notifications to an iOS client given an APNS certificate, key, and pushtoken.

Note: it does not map from account to push token when sending push notifications – it only sends the push notifications using the provided push token:

./examples/apnsworker.py --cert "path/to/cert.pem" --key "path/to/key.pem" --pushtoken "target users hex pushtoken"

twilioworker.py

This worker is similar to apnsworker.py, except instead of sending APNs when a new email arrives, it sends a text message via Twilio:

./examples/twilioworker.py --sid "twilio sid" --token "twilio token" --to "to phone #" --from "from phone #"

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

switchboard-python-0.1.3.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

switchboard-python-0.1.3.macosx-10.9-x86_64.tar.gz (11.0 kB view details)

Uploaded Source

File details

Details for the file switchboard-python-0.1.3.tar.gz.

File metadata

File hashes

Hashes for switchboard-python-0.1.3.tar.gz
Algorithm Hash digest
SHA256 972aa6942e53037747ed892712d06ef8724aaaa3cd28795d4774b58f6ced8cc0
MD5 c5b2cfaba679447c10ac9e63390aa384
BLAKE2b-256 e245adf7063db56dfe6ecb3c1b2de2bbe4dcdbfb836ce620068eac3873aab1e3

See more details on using hashes here.

File details

Details for the file switchboard-python-0.1.3.macosx-10.9-x86_64.tar.gz.

File metadata

File hashes

Hashes for switchboard-python-0.1.3.macosx-10.9-x86_64.tar.gz
Algorithm Hash digest
SHA256 edf142da7c822afce16a30f61ca83b84acf1ffa6c2e316f8efbe22adfcb42e94
MD5 31c3e8f887d975f07e4b2841e6e08b90
BLAKE2b-256 0d2c4237ab08d9a0b73a540ce9514f591e0efad5e4f6e30356a8ce646ce7073d

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