-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
80 lines (68 loc) · 2.17 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Usage: run.py (--host <mq_host>) (--user <mq_user>) (--pass <mq_pass>) (--sys_name <sys_name>) [--debug]
run.py (--help | -h)
Options:
-h --help
-v --debug verbose mode
--host RabbitMQ host
--user RabbitMQ user
--pass RabbitMQ pass
--sys_name System Name
"""
from rpc.synapse.astraea import Synapse
import json
from docopt import docopt
arguments = docopt(__doc__)
def event_test(params, props):
print("**收到EVENT: %s@%s %s" % (props.type, props.reply_to, json.dumps(params)))
return True
def rpc_test(params, props):
print("**RPC请求:", params)
return {"from": "python", "m": params["msg"], "number": 5233}
server = Synapse()
# 定义事件回调
server.event_callback = {
"java.test": event_test,
"dotnet.test": event_test,
"python.test": event_test,
"php.test": event_test,
"ruby.test": event_test,
"node.test": event_test,
}
# #定义RPC服务方法
server.rpc_callback = {
"test": rpc_test,
}
server.sys_name = arguments["<sys_name>"]
server.app_name = "python"
server.mq_host = arguments["<mq_host>"]
server.mq_user = arguments["<mq_user>"]
server.mq_pass = arguments["<mq_pass>"]
if arguments["--debug"]:
server.debug = True
server.serve()
def show_help():
print("----------------------------------------------")
print("| event usage: |")
print("| > event [event] [msg] |")
print("| rpc usage: |")
print("| > rpc [app] [method] [msg] |")
print("----------------------------------------------")
show_help()
while True:
try:
info = input("input >> ")
inputs = info.split(' ')
if inputs[0] == "rpc":
if len(inputs) != 4:
show_help()
continue
print(server.send_rpc(inputs[1], inputs[2], {"msg": inputs[3]}))
elif inputs[0] == "event":
if len(inputs) != 3:
show_help()
continue
server.send_event(inputs[1], {"msg": inputs[2]})
else:
show_help()
except KeyboardInterrupt:
exit()