1 Star 0 Fork 11

切梦刀 / zbus-python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT
            /\\\                                                                             
            \/\\\                                                                            
             \/\\\                                           /\\\\\\\\\     /\\\  /\\\       
 /\\\\\\\\\\\ \/\\\         /\\\    /\\\  /\\\\\\\\\\        /\\\/////\\\   \//\\\/\\\       
 \///////\\\/  \/\\\\\\\\\  \/\\\   \/\\\ \/\\\//////        \/\\\\\\\\\\     \//\\\\\       
       /\\\/    \/\\\////\\\ \/\\\   \/\\\ \/\\\\\\\\\\       \/\\\//////       \//\\\       
      /\\\/      \/\\\  \/\\\ \/\\\   \/\\\ \////////\\\       \/\\\          /\\ /\\\       
     /\\\\\\\\\\\ \/\\\\\\\\\  \//\\\\\\\\\   /\\\\\\\\\\  /\\\ \/\\\         \//\\\\/       
     \///////////  \/////////    \/////////   \//////////  \///  \///           \////      

zbus-python-client

zbus strives to make Message Queue and Remote Procedure Call fast, light-weighted and easy to build your own service-oriented architecture for many different platforms. Simply put, zbus = mq + rpc.

zbus carefully designed on its protocol and components to embrace KISS(Keep It Simple and Stupid) principle, but in all it delivers power and elasticity.

Start zbus, please refer to https://gitee.com/rushmore/zbus

Getting started

pip install zbuspy
  • zbus.py works for both python2.x and python3.x

API Demo

Only demos the gist of API, more configurable usage calls for your further interest.

Produce message

from zbus import MqClient, Message

def onopen(client):
    msg = Message()
    msg.headers.cmd = 'pub'
    msg.headers.mq = 'MyMQ'
    msg.body = 'hello from python'

    client.invoke(msg, lambda res: print(res) )

client = MqClient('localhost:15555')
client.onopen = onopen
client.connect()

Consume message

from zbus import MqClient, Message

mq = 'MyMQ'
channel = 'MyChannel'

def create_mq(client):
    msg = Message()
    msg.headers.cmd = 'create'
    msg.headers.mq = mq
    msg.headers.channel = channel 
    
    client.invoke(msg, lambda res: print(res)) #lambda

def onopen(client):
    
    create_mq(client)
    
    msg = Message()
    msg.headers.cmd = 'sub' #sub on channel of mq
    msg.headers.mq = mq
    msg.headers.channel = channel 
    
    def cb(res):
        print(res)
    
    client.invoke(msg, cb)

def message_handler(msg):
    print(msg)

client = MqClient('localhost:15555')
client.onopen = onopen
client.add_mq_handler(mq, channel, message_handler)
client.connect() 

RPC client

from zbus import RpcClient   
rpc = RpcClient('localhost:15555', mq='MyRpc')

res = rpc.example.plus(1,2) 
print(res)

rpc.close()

RPC service

from zbus import RpcServer, Message, RequestMapping


class MyService: 
    
    def echo(self, ping):
        return str(ping)
    
    def getString(self, s):
        return str(s)
        
    def plus(self, a, b):  
        return int(a) + int(b) 
    
    def testEncoding(self, msg): #msg -- request message
        print(msg)
        return u'中文'
    
    def noReturn(self):
        pass
    
    def home(self):
        res = Message()
        res.status = 200
        res.headers['content-type'] = 'text/html; charset=utf8'
        res.body = '<h1> from Python </h1>'
        return res
    
    def getBin(self):
        b = bytearray(10)
        import base64
        return base64.b64encode(b).decode('utf8')
    
    def throwException(self):
        raise Exception("runtime exception from server")
    
    @RequestMapping(path='/m', method='POST') #change path
    def map(self): 
        return {
            'key1': 'mykey',
            'key2': 'value2',
            'key3': 2.5
        }
        
    def testTimeout(self):
        import time
        time.sleep(20) 


'''
Wait message from zbus(MQ), mount rpc handlers
''' 
p = RpcProcessor() 
p.mount('/', MyService()) #relative mount url 
    
server = RpcServer(p) 
#When auth required
#server.enable_auth('2ba912a8-4a8d-49d2-1a22-198fd285cb06', '461277322-943d-4b2f-b9b6-3f860d746ffd') #apiKey + secretKey
server.mq_server_address = 'localhost:15555'
#server.mq_server_address = '111.230.136.74:15555'
server.mq = '/' #mount to the root url of zbus server
server.mq_mask = Protocol.MASK_DELETE_ON_EXIT#delete on exit
server.start()
The MIT License (MIT) Copyright (c) 2017 rushmore Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

zbus python client 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/oklongmm/zbus-python.git
git@gitee.com:oklongmm/zbus-python.git
oklongmm
zbus-python
zbus-python
master

搜索帮助