Trouble with getitem
Justin M Wozniak <wozniak-zhzY/AYb9nBYTrM/[email protected]> Fri, 18 Jul 2014 10:57:43 -0500
| Newsgroups | gmane.comp.python.pyro |
|---|---|
| Message-ID | <[email protected]> |
Hi
I'm having trouble with Pyro and the getitem syntax. I am
basically trying to use __getitem__ on the proxy. A stand-alone test is
attached.
In short, the server class has:
def __getitem__(self, key):
message("__getitem__")
return self.getitem(key)
and the client has:
proxy = Pyro4.Proxy(uri)
value3 = proxy[key3]
which results in:
TypeError: 'Proxy' object has no attribute '__getitem__'
If anyone can help me get this working it would be greatly appreciated.
Thanks
Justin
--
Justin M Wozniak
------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
Pyro-core mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyro-core
TestClient1.py
(text/x-python, 1.2 KB)
#!/usr/bin/env python
"""
Dummy client for prototyping Pyro
"""
import sys
import Pyro4
def message(msg):
print("pyro client: " + str(msg))
if len(sys.argv) == 1:
uri = raw_input("Enter URI: ")
elif len(sys.argv) == 2:
uri = sys.argv[1]
else:
print "usage: client.py <URI>"
exit(1)
# Get a Pyro proxy to the remote object
Pyro4.config.SERIALIZER = "pickle"
proxy = Pyro4.Proxy(uri)
# Also tried this:
# proxy.__getitem__ = proxy.getitem
b = True
# Use proxy object normally
try:
print proxy.__dict__
print dir(proxy)
# Works:
b = proxy.f1("hello")
# Works:
key1 = "key1"
value1 = proxy.getitem(key1)
message("got: %s:%s" % (key1, value1))
# Works:
key2 = "key2"
value2 = proxy.__getitem__(key2)
message("got: %s:%s" % (key2, value2))
# Doesn't work:
key3 = "key3"
value3 = proxy[key3]
message("got: %s:%s" % (key3, value3))
except Exception as e:
print "Caught exception during remote operations!"
print("Exception message: " + str(e))
print "Pyro remote traceback:"
print "".join(Pyro4.util.getPyroTraceback())
message("Shutting down service...")
proxy.exit(0)
if b:
message("Success.")
else:
message("Failed!")
exit(1)
exit(0)
TestService1.py
(text/x-python, 1.1 KB)
#!/usr/bin/env python
import Pyro4
import sys
import threading
import time
def shutdown():
time.sleep(1)
daemon.shutdown()
def message(msg):
print("pyro server: " + msg)
class TestService1:
def f1(self, a):
message("f1(%s)" % str(a))
return True
def __getitem__(self, key):
message("__getitem__")
return self.getitem(key)
def getitem(self, key):
message("getitem inputs: " + str(key))
t = 42
message("getitem result: " + str(t))
return t
def exit(self,code):
message("Daemon exiting...")
thread = threading.Thread(target=shutdown)
thread.setDaemon(True)
thread.start()
service = TestService1()
# Make an empty Pyro daemon
Pyro4.config.SERIALIZERS_ACCEPTED.add("pickle")
daemon = Pyro4.Daemon(host=None, port=8080)
# Register the object as a Pyro object
uri = daemon.register(service, 0)
# Print the URI so we can use it in the client later
print("URI: " + str(uri))
sys.stdout.flush()
# Start the event loop of the server to wait for calls
daemon.requestLoop()
daemon.close()
message("Daemon exited.")