Commits

William Pitcock committed 16d89ab

Add RPC client stub.

  • Participants
  • Parent commits f90fdaa

Comments (0)

Files changed (1)

File 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 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):
+        envelope = rpc_message.encode(self._secret, name, **kwargs)
+        response = rpc_message.encode(self._secret, None, hello=name)
+
+        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('127.0.0.1', 5959, 'moocows')
+    print proxy.ping(moo='cow')