<br><div><span class="gmail_quote">2006/10/18, Jean-Paul Calderone &lt;<a href="mailto:exarkun@divmod.com">exarkun@divmod.com</a>&gt;:</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 &lt;<a href="mailto:felipe.lessa@gmail.com">felipe.lessa@gmail.com</a>&gt; wrote:<br>&gt;2006/10/17, Franz Zieher &lt;<a href="mailto:franz.zieher@gmail.com">franz.zieher@gmail.com
</a>&gt;:<br>&gt;&gt;<br>&gt;&gt;Can anybody provide a an example on how I would best send<br>&gt;&gt;a data structure (i.e. an ElemenTree from elementtree) over from<br>&gt;&gt;a server to a client process. I followed somewhat the example in
<br>&gt;&gt;the documentation and used the perspective broker &quot;pb&quot; to send<br>&gt;&gt;an &quot;simple&quot; structure (some class with simple members)<br>&gt;&gt;<br>&gt;&gt;When I changed to a more complex class definition, I obviously got
<br>&gt;&gt;an InsecureJelly exception.<br>&gt;&gt;<br>&gt;&gt;I'm sure this question must have come up a number of times. I'm new<br>&gt;&gt;in using twisted and not everything is so obvious at the beginning to me<br>&gt;&gt;:-(
<br>&gt;<br>&gt;I don't know if it may help you, but I think you should take a look at<br>&gt;Cerealizer ( <a href="http://home.gna.org/oomadness/en/cerealizer/index.html">http://home.gna.org/oomadness/en/cerealizer/index.html
</a> )<br>&gt;<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>&nbsp; def __init__(self):<br>&nbsp;&nbsp;&nbsp; x = 3.<br>&nbsp;&nbsp;&nbsp; y = 4.<br>&nbsp;&nbsp;&nbsp; data = ET.ElementTree()<br><br>class Bag:<br>&nbsp; def setContent(self,content):<br>&nbsp;&nbsp;&nbsp; # content must be serializable
<br>&nbsp;&nbsp;&nbsp; s_content = StringIO()<br>&nbsp;&nbsp;&nbsp; pickle.dump(content,s_content)<br>&nbsp;&nbsp;&nbsp; s_content.flush()<br>&nbsp;&nbsp;&nbsp; self.content = compress(s_content.getvalue())<br><br>class CopyBag(Bag, pb.Copyable):<br>&nbsp; pass<br><br>class Sender(pb.Root
):<br>&nbsp; def __init__(self, bag):<br>&nbsp;&nbsp;&nbsp; self.bag = bag<br><br>&nbsp; def remote_sendBag(self, remote):<br>&nbsp;&nbsp;&nbsp; self.remote = remote<br>&nbsp;&nbsp;&nbsp; d = remote.callRemote(&quot;takeBag&quot;, self.bag)<br>&nbsp;&nbsp;&nbsp; d.addCallback(self.ok).addErrback(
self.notOk)<br><br>&nbsp; def ok(self, response):<br>&nbsp;&nbsp;&nbsp; print &quot;info: %s&quot; % response<br>&nbsp;&nbsp;&nbsp; d = self.remote.callRemote(&quot;finishTransfer&quot;)<br>&nbsp;&nbsp;&nbsp; d.addErrback(self.notOk)<br>&nbsp;&nbsp;&nbsp; return None<br><br>&nbsp; def notOk(self, failure):
<br>&nbsp;&nbsp;&nbsp; if failure.type == jelly.InsecureJelly:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;error: InsecureJelly&quot;<br>&nbsp;&nbsp;&nbsp; elif failure.type == pb.PBConnectionLost:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;info: data transfer finished&quot;<br>&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp; print failure
<br>&nbsp;&nbsp;&nbsp; return None<br><br>def main():<br>&nbsp; from pbsimple import CopyBag<br><br>&nbsp; icae = iCAEconfig()<br>&nbsp; bag = CopyBag()<br><br>&nbsp; bag.setContent(test())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; sender = Sender(bag)<br><br>&nbsp; factory = pb.PBServerFactory
(sender)<br>&nbsp; reactor.listenTCP(8789, factory)<br>&nbsp; reactor.run()<br><br>if __name__ == '__main__':<br>&nbsp; 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>&nbsp; pass<br><br>pb.setUnjellyableForClass(CopyBag, ReceiverBag)<br><br>class Receiver(pb.Referenceable):<br>&nbsp; def remote_takeBag(self, bag):<br>&nbsp;&nbsp;&nbsp; # deserialize the bag.content<br>&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.content
 = pickle.loads(decompress(bag.content))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.len = len(bag.content)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &quot;data arrived safe and sound&quot; # positive acknowledgement<br>&nbsp;&nbsp;&nbsp; except:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.content = None<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.len
 = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;error: data could not be de-serialized&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reactor.stop()<br><br>&nbsp; def remote_finishTransfer(self):<br>&nbsp;&nbsp;&nbsp; print &quot;got bag of length:&quot;, self.len<br>&nbsp;&nbsp;&nbsp; reactor.stop()<br><br>&nbsp; def requestBag(self,remote):
<br>&nbsp;&nbsp;&nbsp; print &quot;asking server for sending the data bag&quot;<br>&nbsp;&nbsp;&nbsp; remote.callRemote(&quot;sendBag&quot;,self)<br><br>def getContentFromServer():<br><br>&nbsp; receiver = Receiver()<br>&nbsp; factory = pb.PBClientFactory()<br>
&nbsp; reactor.connectTCP(&quot;localhost&quot;, 8789, factory,30)<br>&nbsp; d = factory.getRootObject()<br>&nbsp; d.addCallback(receiver.requestBag)<br>&nbsp; reactor.run()<br>&nbsp; return receiver.content<br><br>if __name__ == '__main__':<br>&nbsp; content = getContentFromServer()
<br>&nbsp; if hasattr(content,'getroot'):<br>&nbsp;&nbsp;&nbsp; ET.dump(content)<br>&nbsp; else:<br>&nbsp;&nbsp;&nbsp; print content<br><br><br><br><br><br><span class="gmail_quote"></span>