<br><div><span class="gmail_quote">2006/10/18, Jean-Paul Calderone <<a href="mailto:exarkun@divmod.com">exarkun@divmod.com</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Tue, 17 Oct 2006 22:51:55 -0200, Felipe Almeida Lessa <<a href="mailto:felipe.lessa@gmail.com">felipe.lessa@gmail.com</a>> wrote:<br>>2006/10/17, Franz Zieher <<a href="mailto:franz.zieher@gmail.com">franz.zieher@gmail.com
</a>>:<br>>><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>><br>>I don't know if it may help you, but I think you should take a look at<br>>Cerealizer ( <a href="http://home.gna.org/oomadness/en/cerealizer/index.html">http://home.gna.org/oomadness/en/cerealizer/index.html
</a> )<br>><br><br>Using this together with PB would probably be counterproductive.<br><br>Jean-Paul<br><br>_______________________________________________<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com">
Twisted-Python@twistedmatrix.com</a><br><a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br></blockquote></div><br><br>Thanks for the help. I opted for Jean-Pauls suggestion.
<br>It really allows for such a simple data transfer between a server<br>and a client. I included a compressed string. It very likely misses<br>a number of details, I post the changes again. Maybe it is helpful<br>for somebody else.
<br><br>Franz :-)<br><br>----------- pbsimple.py<br><br>from twisted.spread import pb, jelly<br>from twisted.internet import reactor<br>import elementtree.ElementTree as ET<br>from path import path<br>import os, pickle<br>
from StringIO import StringIO<br>from zlib import compress<br><br>class test:<br> def __init__(self):<br> x = 3.<br> y = 4.<br> data = ET.ElementTree()<br><br>class Bag:<br> def setContent(self,content):<br> # content must be serializable
<br> s_content = StringIO()<br> pickle.dump(content,s_content)<br> s_content.flush()<br> self.content = compress(s_content.getvalue())<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(test()) <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>----- pbsimpleclient.py<br><br>from twisted.spread import pb<br>from twisted.internet import reactor<br>
from twisted.python import util<br>import os, pickle<br>from StringIO import StringIO<br>from zlib import decompress<br>import elementtree.ElementTree as ET<br><br>from pbsimple import Bag, CopyBag, test<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> # deserialize the bag.content<br> try:<br> self.content
= pickle.loads(decompress(bag.content))<br> self.len = len(bag.content)<br> return "data arrived safe and sound" # positive acknowledgement<br> except:<br> self.content = None<br> self.len
= 0<br> print "error: data could not be de-serialized"<br> reactor.stop()<br><br> def remote_finishTransfer(self):<br> print "got bag of length:", self.len<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.content<br><br>if __name__ == '__main__':<br> content = getContentFromServer()
<br> if hasattr(content,'getroot'):<br> ET.dump(content)<br> else:<br> print content<br><br><br><br><br><br><span class="gmail_quote"></span>