0

I'm trying to understand how to make RPC calls using Python. I have a stupid server that defines a class and exposes a method that create instances of that class:

# server.py
class Greeter(object):
    def __init__(self, name):
        self.name = name
    def greet(self):
        return "Hi {}!".format(self.name)

def greeter_factory(name):
    return Greeter(name)

some_RPC_framework.register(greeter_factory)

and a client that wants to get an instance of the Greeter:

# client.py
greeter_factory = some_RPC_framework.proxy(uri_given_by_server)

h = greeter_factory("Heisemberg")
print("Server returned:", h.greet())

The problem is that I've found no framework that allows to return instances of user-defined objects, or that only returns a dict with the attributes of the object (for example, Pyro4).

In the past I've used Java RMI, where you can specify a codebase on the server where the client can download the compiled classes, if it needs to. Is there something like this for Python? Maybe some framework that can serialize objects along with the class bytecode to let the client have a full-working instance of the class?

Alessio
  • 341
  • 2
  • 5
  • 7

2 Answers2

0

Pyro can do this to a certain extent. You can register custom class (de)serializers when using the default serializer. Or you can decide to use the pickle serializer, but that has severe security implications. See http://pythonhosted.org/Pyro4/clientcode.html#serialization

What Pyro won't do for you, even when using the pickle serializer, is transfer the actual bytecode that makes up the module definition. The client, in your case, has to be able to import the module defining your classes in the regular way. There's no code transportation.

Irmen de Jong
  • 2,739
  • 1
  • 14
  • 26
0

You can consider using

payload = CPickle.dump(Greeter(name))

on server side and on client side once the payload is received do -

h = CPickle.load(payload)

to get the instance of Greeter object that server has created.

Donald Alan
  • 61
  • 1
  • 2