蟒蛇开关
switchboard-python的Python项目详细描述
Switchboardpython提供了编写交换机工作线程的帮助程序 以及python中的客户端。
安装
从[pypi](https://pypi.python.org/pypi)安装这个库最简单:
pip install switchboard-python
从源代码生成:
# Building ./setup.py build
# Running the tests ./setup.py test
# Development install pip install -e .
# Actual install pip install .
用法
switchboard.Client
类用于与
总机工作人员和客户。
假设交换机应用程序正在运行,则
示例通过工作接口打开到服务器的连接,
并使用connect
命令发送批处理请求(请参见
interfaces guide用于命令文档)。
worker=switchboard.Client("ws://192.168.50.2:8080/workers")worker.connect()worker.send_cmds(("connect",CONN_SPEC))worker.run_forever()
要处理命令响应,send_cmds
返回一个promise
当命令的
响应到达,其中cmds
是给定给
send_cmds
,并且resps
是返回的响应列表
通过总机。
defhandle_get_mailboxes((cmds,resps)):print"For cmds",cmds,", received resps:",respsworker.send_cmds(("getMailboxes",{}).then(handle_get_mailboxes)
在connect上添加命令和/或处理未经请求的消息
子类化基switchboard.Client
–一个未经请求的消息
不是响应命令发送,而是在服务器有新的
信息,如收到新邮件
classTheWorker(switchboard.Client):defopened(self):print"Connected to Switchboard, issuing watchAll cmd."worker.send_cmds(("watchAll",{}))defreceived_unsolicited(resps):print"Received unsolicited resps from server:",respsforrespinresps:ifresp[0]=='newMessage':print"New message:",resp[1]worker=TheWorker("ws://127.0.0.1:8080/workers")worker.connect()worker.run_forever()
示例
所有示例都位于/examples
下。每个示例都使用
命令行参数,您可以通过:
./examples/example.py --help
侦听器.py
这个worker提供了一个简单的双向通信示例
使用交换机工作接口。它监听总机
通知它新邮件,然后获取原始邮件并解析
它使用pythonemail
模块:
./examples/listener.py
呼吸暂停器.py
此工作进程向ios发送新电子邮件Apple Push Notifications 客户端提供了APNS证书、密钥和PushToken。
注意:当发送push时,不从帐户映射到push令牌 通知–它只使用 提供的推送令牌:
./examples/apnsworker.py --cert "path/to/cert.pem" --key "path/to/key.pem" --pushtoken "target users hex pushtoken"
斜纹工人.py
此工作进程与apnsworker.py
类似,只是不发送
当新邮件到达时,它通过
Twilio:
./examples/twilioworker.py --sid "twilio sid" --token "twilio token" --to "to phone #" --from "from phone #"