:octocat: use tornado to achieve rpc server
swift_rpc是用tornado实现的rpc服务,而且支持多种方式加密传输, 现在开放了四个调度接口:
Change Log:
Version: 2.1
Version: 2.2
Version: 2.3
Version: 2.4
Version: 2.5
Future:
测试json body的args,kwargs:
curl -H "Content-Type: application/json" -H "User-Agent: swift_rpc" -X GET -d '{"args":"[123,456]","kwargs":{"name":1}}' http://localhost:8080/test_args
curl -H "Content-Type: application/json" -H "User-Agent: swift_rpc" -X GET -d '{"args":"123"}' http://localhost:8080/test
测试选择加密模式:
curl -H "Encryption: base64" -H "Content-Type: application/json" -H "User-Agent: swift_rpc" -X GET -d '{"args": [123, 456], "kwargs": {"name": 1}}' http://localhost:8080/test_args
测试arguments的args,kwargs:
curl -H "User-Agent: swift_rpc" -X GET -d "args=123" http://localhost:8080/test
Tornado RPC Server Usage:
#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import time
from tornado import gen
from swift_rpc.server import RPCServer
from swift_rpc.mq import rq_conn
from swift_rpc.mq import redis_conn
from swift_rpc.mq import fetch
from config import *
from api import *
def test(args):
return "this is test %s"%(args)
def test_args(a,b,name='xiaorui.cc'):
print a,b,name
return "this is test %s %s"%(a,name)
def get_result(job_id):
return redis_conn.hgetall(job_id)
def test_block(args):
time.sleep(5)
return "You said "
@gen.coroutine
def test_async(arg):
return gen.Return("this is test_async async %s" % arg)
if __name__ == "__main__":
server = RPCServer()
server.register(test)
server.register(test_args)
server.register(get_result)
server.register_async(test_async)
server.register_pool(test_block)
server.register_mq(test_mq)
server.register_mq(go)
server.start(RPC_HOST,RPC_PORT)
swift_rpc client Usage:
from swift_rpc.client import RPCClient
client = RPCClient('localhost:8080')
print client.test('hi')
print client.test_args('welcome','xiaorui.cc',name='nima')