Newer
Older
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
#!/usr/bin/env python
'''
Server mode using Gevent.
'''
from __future__ import absolute_import
import sys
import random
from sjrpc.server import GreenRpcServer
from sjrpc.utils import RpcHandler, pass_connection
class MyHandler(RpcHandler):
def random(self, min=0, max=100):
return random.randint(min, max)
@pass_connection
def proxy(self, conn, method, *args, **kwargs):
'''
Example of bidirectionnal RPC. When the peer call this method, the
server forward the call of the specified method the peer, and return
returned value.
'''
return conn.call(method, *args, **kwargs)
# Get arguments from the command line:
if len(sys.argv) < 3:
print 'Usage: %s <listening address> <listening port>' % sys.argv[0]
sys.exit(2)
address = sys.argv[1]
port = int(sys.argv[2])
# Create the server instance:
rpcserver = GreenRpcServer(address, port, conn_kw=dict(handler=MyHandler()))
# conn_kw (and conn_args) are arguments which are automatically passed on
# client RpcConnection instances.
rpcserver.run()