1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
from __future__ import print_function
import Pyro4
from Pyro4.util import SerializerBase
import mycustomclasses
# use serpent
Pyro4.config.SERIALIZER = "serpent"
# register the special serialization hooks
def thingy_class_to_dict(obj):
print("{serializer hook, converting to dict: %s}" % obj)
return {
"__class__": "waheeee-custom-thingy",
"number-attribute": obj.number
}
def thingy_dict_to_class(classname, d):
print("{deserializer hook, converting to class: %s}" % d)
return mycustomclasses.Thingy(d["number-attribute"])
def otherthingy_dict_to_class(classname, d):
print("{deserializer hook, converting to class: %s}" % d)
return mycustomclasses.OtherThingy(d["number"])
# for 'Thingy' we register both serialization and deserialization hooks
SerializerBase.register_dict_to_class("waheeee-custom-thingy", thingy_dict_to_class)
SerializerBase.register_class_to_dict(mycustomclasses.Thingy, thingy_class_to_dict)
# for 'OtherThingy' we only register a deserialization hook (and for serialization depend on serpent's default behavior)
SerializerBase.register_dict_to_class("mycustomclasses.OtherThingy", otherthingy_dict_to_class)
# regular Pyro server stuff
@Pyro4.expose
class Server(object):
def method(self, arg):
print("\nmethod called, arg=", arg)
response = mycustomclasses.Thingy(999)
return response
def othermethod(self, arg):
print("\nothermethod called, arg=", arg)
response = mycustomclasses.OtherThingy(999)
return response
Pyro4.core.Daemon.serveSimple(
{
Server: "example.customclasses"
},
ns=False)
|