1. TortoiseLabs LLC
  2. Untitled project
  3. ediarpc


ediarpc / rpc_client.py

#!/usr/bin/env python
Copyright (c) 2012, 2013 TortoiseLabs LLC

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

This software is provided 'as is' and without any warranty, express or
implied. In no event shall the authors be liable for any damages arising
from the use of this software.

import socket
import rpc_message

class ServerProxy(object):
    def __init__(self, host, port, secret, allow_untrusted=False):
        self._host = host
        self._port = port
        self._secret = secret
        self._allow_untrusted = allow_untrusted

    def _call(self, name, **kwargs):
        sock = socket.create_connection((self._host, self._port))
        envelope = rpc_message.encode(self._secret, name, **kwargs) + '\r\n'
        response = sock.recv(4096)

        return rpc_message.decode(self._secret, response, self._allow_untrusted)[1]

    def __getattr__(self, name):
        Create a function which makes a RPC call.
        def f(*args, **kwargs):
            return self._call(name, **kwargs)

        return f

if __name__ == '__main__':
    proxy = ServerProxy('', 5959, 'moocows')
    print proxy.ping(moo='cow')