Skip to content

Commit f8d3257

Browse files
committed
add content-Type support
1 parent 140f508 commit f8d3257

File tree

5 files changed

+71
-55
lines changed

5 files changed

+71
-55
lines changed

README.md

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,72 +21,78 @@ Version: 2.3
2121
1. 解决了curl调用rq的get_result时出现的0x80 code异常
2222

2323
Future:
24-
1. 使用rsa保证rpc通信安全
25-
2. swift_rpc完善RQ异步任务队列
26-
3. 增加request json body的识别,在这基础上做安全的封装
24+
1. 统一配置配置文件
25+
2. 使用rsa保证rpc通信安全
26+
3. swift_rpc完善RQ异步任务队列
27+
4. 增加request json body的识别,在这基础上做安全的封装
28+
29+
30+
测试json body的args,kwargs:
31+
```
32+
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
33+
curl -H "Content-Type: application/json" -H "User-Agent: swift_rpc" -X GET -d '{"args":"123"}' http://localhost:8080/test
34+
```
35+
36+
测试arguments的args,kwargs:
37+
```
38+
curl -H "User-Agent: swift_rpc" -X GET -d "args=123" http://localhost:8080/test
39+
```
2740

2841
Tornado RPC Server Usage:
2942

3043
```
3144
#coding:utf-8
45+
import sys
46+
reload(sys)
47+
sys.setdefaultencoding("utf-8")
48+
3249
import time
3350
from tornado import gen
3451
from swift_rpc.server import RPCServer
52+
from swift_rpc.mq import rq_conn
53+
from swift_rpc.mq import redis_conn
54+
from swift_rpc.mq import fetch
3555
from config import *
56+
from api import *
57+
58+
def test(args):
59+
return "this is test %s"%(args)
3660
61+
def test_args(a,b,name='xiaorui.cc'):
62+
print a,b,name
63+
return "this is test %s %s"%(a,name)
3764
38-
def test(args,**kwargs):
39-
return "You said %s" % args
65+
def get_result(job_id):
66+
return redis_conn.hgetall(job_id)
4067
4168
def test_block(args):
4269
time.sleep(5)
4370
return "You said "
4471
4572
@gen.coroutine
4673
def test_async(arg):
47-
raise gen.Return("You said async %s" % arg)
48-
49-
def test_mq(arg):
50-
print 'mq...'
51-
return "You said "
52-
53-
server = RPCServer()
54-
server.register(test)
55-
server.register_async(test_async)
56-
server.register_pool(test_block)
57-
server.register_mq(test_mq)
58-
server.start(RPC_HOST,RPC_PORT)
74+
return gen.Return("this is test_async async %s" % arg)
75+
76+
if __name__ == "__main__":
77+
server = RPCServer()
78+
server.register(test)
79+
server.register(test_args)
80+
server.register(get_result)
81+
server.register_async(test_async)
82+
server.register_pool(test_block)
83+
server.register_mq(test_mq)
84+
server.register_mq(go)
85+
server.start(RPC_HOST,RPC_PORT)
5986
```
6087

6188
swift_rpc client Usage:
6289

6390
```
64-
#coding:utf-8
65-
import time
66-
from tornado import gen
67-
from swift_rpc.server import RPCServer
68-
from config import *
69-
70-
71-
def test(args,**kwargs):
72-
return "You said %s" % args
73-
74-
def test_block(args):
75-
time.sleep(5)
76-
return "You said "
91+
from swift_rpc.client import RPCClient
7792
78-
@gen.coroutine
79-
def test_async(arg):
80-
raise gen.Return("You said async %s" % arg)
93+
client = RPCClient('localhost:8080')
94+
print client.test('hi')
95+
print client.test_args('welcome','xiaorui.cc',name='nima')
96+
```
8197

82-
def test_mq(arg):
83-
print 'mq...'
84-
return "You said "
8598

86-
server = RPCServer()
87-
server.register(test)
88-
server.register_async(test_async)
89-
server.register_pool(test_block)
90-
server.register_mq(test_mq)
91-
server.start(RPC_HOST,RPC_PORT)
92-
```

swift_rpc/client/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
from urlparse import urljoin
44

55
class _RPC(object):
6-
__HEADERS__ = {'User-Agent': 'swift_rpc'}
6+
__HEADERS__ = {'User-Agent': 'swift_rpc','Content-Type':'application/json'}
77

88
def __init__(self, server, name):
99
self._name = name
1010
self._url = urljoin(server, name)
1111

1212
def __call__(self, *args, **kwargs):
13-
if args:
14-
# the server will turn this list into
15-
# positional args
16-
kwargs['args'] = args
17-
13+
params = {}
14+
params['args'] = args
15+
params['kwargs'] = kwargs
1816
try:
19-
resp = requests.get(self._url, data=kwargs, headers=self.__HEADERS__)
17+
resp = requests.get(self._url, data=json.dumps(params), headers=self.__HEADERS__)
2018
except Exception as e:
2119
raise RPCClient.FailedCall(e)
2220

swift_rpc/server/handlers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#coding:utf-8
2+
import json
23
from tornado import gen, log, web
34
from tornado.concurrent import run_on_executor
45
from concurrent.futures import ThreadPoolExecutor
@@ -42,10 +43,15 @@ def prepare(self):
4243
def args_kwargs(self):
4344
args = []
4445
# support positional arguments
46+
if self.request.headers.get('Content-Type') == "application/json":
47+
data = json.loads(self.request.body)
48+
args = data.get('args',[])
49+
kwargs = data.get('kwargs',{})
50+
raise gen.Return((args, kwargs))
51+
4552
if 'args' in self.request.arguments:
4653
args = self.request.arguments['args']
4754
del self.request.arguments['args']
48-
# keyword arguments get passed as a list so extract them
4955
kwargs = dict([(k, v[0]) for k, v in self.request.arguments.items()])
5056
raise gen.Return((args, kwargs))
5157

@@ -97,6 +103,7 @@ class _MessageQueueBase(_Handler):
97103
@gen.coroutine
98104
def get(self):
99105
args, kwargs = yield self.args_kwargs()
106+
print args,kwargs
100107
try:
101108
job = rq_conn.enqueue('api.'+self.func[0].func_name,*args,**kwargs)
102109
"""

test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
client = RPCClient('localhost:8080')
44
print client.test('hi')
5-
print client.test_async('hi')
5+
print client.test_args('welcome','xiaorui.cc',name='nima')

test_server.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
from config import *
1313
from api import *
1414

15-
def test(args,**kwargs):
16-
return "You said %s" % args
15+
def test(args):
16+
return "this is test %s"%(args)
17+
18+
def test_args(a,b,name='xiaorui.cc'):
19+
print a,b,name
20+
return "this is test %s %s"%(a,name)
1721

1822
def get_result(job_id):
1923
return redis_conn.hgetall(job_id)
@@ -24,11 +28,12 @@ def test_block(args):
2428

2529
@gen.coroutine
2630
def test_async(arg):
27-
return gen.Return("You said async %s" % arg)
31+
return gen.Return("this is test_async async %s" % arg)
2832

2933
if __name__ == "__main__":
3034
server = RPCServer()
3135
server.register(test)
36+
server.register(test_args)
3237
server.register(get_result)
3338
server.register_async(test_async)
3439
server.register_pool(test_block)

0 commit comments

Comments
 (0)