<br>Can anybody provide a an example on how I would best send <br>a data structure (i.e. an ElemenTree from elementtree) over from<br>a server to a client process. I followed somewhat the example in<br>the documentation and used the perspective broker "pb" to send
<br>an "simple" structure (some class with simple members)<br><br>When I changed to a more complex class definition, I obviously got<br>an InsecureJelly exception. <br><br>I'm sure this question must have come up a number of times. I'm new
<br>in using twisted and not everything is so obvious at the beginning to me :-(<br><br>Many thanks for the help<br><br>Franz<br><br>Attached is the sample code I used:<br><br>sending test1 --> OK<br>sending test2 --> InsecureJelly exception
<br><br>------ pbsimple.py (server) -------<br>from twisted.spread import pb, jelly<br>from twisted.internet import reactor<br><br>class test:<br> def __init__(self):<br> x = 3.<br> y = 4.<br><br>class Bag:<br> def setContent(self,content):
<br> self.content = content<br> def printContent(self):<br> print "My data is", self.content<br><br>class CopyBag(Bag, pb.Copyable):<br> pass<br><br>class Sender(pb.Root):<br> def __init__(self, bag):<br>
self.bag = bag<br><br> def remote_sendBag(self, remote):<br> self.remote = remote<br> d = remote.callRemote("takeBag", self.bag)<br> d.addCallback(self.ok).addErrback(self.notOk)<br><br> def ok(self, response):
<br> print "info: %s" % response<br> d = self.remote.callRemote("finishTransfer")<br> d.addErrback(self.notOk)<br> return None<br><br> def notOk(self, failure):<br> if failure.type == jelly.InsecureJelly
:<br> print "error: InsecureJelly"<br> elif failure.type == pb.PBConnectionLost:<br> print "info: data transfer finished"<br> else:<br> print failure<br> return None<br><br>def main():
<br> from pbsimple import CopyBag<br><br> icae = iCAEconfig()<br> bag = CopyBag()<br><br> bag.setContent('simple data') # --> OK<br> #bag.setContent(test()) # --> InsecureJelly<br> sender = Sender(bag)<br>
<br> factory = pb.PBServerFactory(sender)<br> reactor.listenTCP(8789, factory)<br> reactor.run()<br><br>if __name__ == '__main__':<br> main()<br><br>-----------------------------<br><br>------------ pbsimleclient.py -----------------
<br><br>from twisted.spread import pb<br>from twisted.internet import reactor<br>from twisted.python import util<br><br>from pbsimple import Bag, CopyBag<br><br>class ReceiverBag(Bag,pb.RemoteCopy):<br> pass<br><br>pb.setUnjellyableForClass
(CopyBag, ReceiverBag)<br><br>class Receiver(pb.Referenceable):<br> def remote_takeBag(self, bag):<br> self.bag = bag<br> return "data arrived safe and sound" # positive acknowledgement<br><br> def remote_finishTransfer(self):
<br> print "got bag:", self.bag<br> reactor.stop()<br><br> def requestBag(self,remote):<br> print "asking server for sending the data bag"<br> remote.callRemote("sendBag",self)<br>
<br>def getContentFromServer():<br><br> receiver = Receiver()<br> factory = pb.PBClientFactory()<br> reactor.connectTCP("localhost", 8789, factory,30)<br> d = factory.getRootObject()<br> d.addCallback(receiver.requestBag
)<br> reactor.run()<br> return receiver.bag.content<br><br>if __name__ == '__main__':<br> print getContentFromServer()<br><br>---------------------------------------------------------<br><br>