pyro4返回复杂的对象

时间:2014-09-03 18:04:24

标签: python python-2.7 pyro

有人可以解释一下,我怎样才能在pyro4中返回一个复杂的对象? ,这是我的代码......和输出。 我的cody很简单,我只想在客户端返回复杂的对象。我不想只返回那些字符串,数字或者somthings。 SERVER(greeting.py)

import Pyro4
from lib import obj

class GreetingMaker(object):
    def get_obj():
        return obj()
    factory = staticmethod(get_obj)


if __name__=="__main__":
    greeting_maker=GreetingMaker()
    daemon=Pyro4.Daemon()                 # make a Pyro daemon
    ns=Pyro4.locateNS()                   # find the name server
    uri=daemon.register(greeting_maker)   # register the greeting object as a Pyro object
    ns.register("example.greeting", uri)  # register the object with a name in the name server

    print "Ready."
    daemon.requestLoop()                  # start the event loop of the server to wait for calls

LIB(lib.py)

class obj(object):
    dato = 'thing'

客户端(client.py)

import Pyro4
from lib import obj
if __name__=="__main__":
    greeting_maker=Pyro4.Proxy("PYRONAME:example.greeting")    # use name server object lookup uri      shortcut
    a = greeting_maker.factory()

OUTPUT客户端

Traceback (most recent call last):
  File "client.py", line 6, in <module>
    a = greeting_maker.factory()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Pyro4/core.py", line 168, in __call__
    return self.__send(self.__name, args, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Pyro4/core.py", line 366, in _pyroInvoke
    data = serializer.deserializeData(msg.data, compressed=msg.flags & message.FLAGS_COMPRESSED)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Pyro4/util.py", line 162, in deserializeData
    return self.loads(data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Pyro4/util.py", line 461, in loads
    return self.recreate_classes(serpent.loads(data))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Pyro4/util.py", line 370, in recreate_classes
    return self.dict_to_class(literal)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Pyro4/util.py", line 349, in dict_to_class
    raise Pyro4.errors.ProtocolError("unsupported serialized class: " + classname)
 Pyro4.errors.ProtocolError: unsupported serialized class: lib.obj

我想在客户端返回复杂的对象:/

感谢


代码刷新:) 现在我还有其他错误:S

1 个答案:

答案 0 :(得分:0)

正在定义类objecto并在同一文件中使用。当文件运行时,您会注意到它的一些有趣之处:它的模块名称设置为__main__。例如,试试这个......

文件1(test_module.py):

class test(object):
    pass

if __name__ == '__main__':
    a = test()
    print a.__class__

接下来,创建第二个文件:

import test_module

a = test_module.test()
print a.__class__

您会看到类名以其模块名称为前缀。作为安全预防措施,Pyro4不允许运行在__main__范围内创建的对象。 objectoGreetingMaker应该放在他们自己的文件中。

您可以在此处详细了解Python的命名空间:https://docs.python.org/2/tutorial/classes.html

你可以在这里看到错误的原因:https://github.com/irmen/Pyro4/blob/master/src/Pyro4/util.py#L291-L298