Serialize python objects along with class definition
Question: 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?


Answer: 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.

