-2

Could someone explain me, how could i return a complex object in pyro4? , here is my code....and the output. My cody is simple, i just want to return complex objects in the client. I dont't want to return only strings, numbers or somthings like that. 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(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 client

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

I want to return objects complex in the client :/

thanks


code refresh :) now i have other error :S

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
xXcoronaXx
  • 13
  • 1
  • 8

1 Answers1

0

The class objecto is being defined and used in the same file. When a file runs, you will notice something interesting about it: its module name is set to __main__. For example, try this...

File 1 (test_module.py):

class test(object):
    pass

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

Next, create a second file:

import test_module

a = test_module.test()
print a.__class__

You'll see that the classname is prefixed with its module name. As a security precaution, Pyro4 doesn't allow running objects created within the __main__ scope. objecto and GreetingMaker should go in a file of their own.

You can learn more about Python's namespaces here: https://docs.python.org/2/tutorial/classes.html

And you can see why the error is thrown here: https://github.com/irmen/Pyro4/blob/master/src/Pyro4/util.py#L291-L298

John M.
  • 2,234
  • 4
  • 24
  • 36
  • thanks for the explanation, but i really want to know how can i return a complex object (server --> client)? – xXcoronaXx Sep 03 '14 at 18:54
  • @corona Do you expect the code you provided to do that? If so, fix the error (which I explained how to do) and see if it works. – John M. Sep 03 '14 at 19:36
  • done, i dont know if i could return a complex object to the client, could u explain me how can i do it please – xXcoronaXx Sep 03 '14 at 21:37
  • @corona once again, do you expect the code you provided to do that? I'm unsure what you mean by a "complex object." You're going to need to explain your problem clearly, as it seems like you're saying your original problem was not what you showed in your question. – John M. Sep 03 '14 at 21:49
  • @corona Your object is not serializable. Look at the documentation of Pyro4 to find out how to make it serializable or what serializer to use: https://pythonhosted.org/Pyro4/clientcode.html – John M. Sep 03 '14 at 21:53
  • when i've said complex object, i wanted to say an object that is not a string, number or array of numbers.... i want to return to the client an object with methods and atributes. Sorry i'm new.... – xXcoronaXx Sep 03 '14 at 22:00
  • @corona Did you look at the link I sent you? It explains different serialization methods you can use, and you can look into what they support. The method Pyro4 uses by default is `serpent`. You may try `pickle`, or look into what you need to do to support your custom class with `serpent`. Once again, the link is: https://pythonhosted.org/Pyro4/clientcode.html#serialization – John M. Sep 04 '14 at 13:58
  • @corona: I suggest you read through the tutorial: https://pythonhosted.org/Pyro4/tutorials.html – John M. Sep 04 '14 at 14:05