Skip to content
server.py 1.05 KiB
Newer Older
#!/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()