[Twisted-Python] Newbie question on how to send "complex" objects over the wire

Franz Zieher franz.zieher at gmail.com
Tue Oct 17 20:05:28 EDT 2006

Can anybody provide a an example on how I would best send
a data structure (i.e. an ElemenTree from elementtree) over from
a server to a client process. I followed somewhat the example in
the documentation and used the perspective broker "pb" to send
an "simple" structure (some class with simple members)

When I changed to a more complex class definition, I obviously got
an InsecureJelly exception.

I'm sure this question must have come up a number of times. I'm new
in using twisted and not everything is so obvious at the beginning to me :-(

Many thanks for the help


Attached is the sample code I used:

sending test1 --> OK
sending test2 --> InsecureJelly exception

------   pbsimple.py (server) -------
from twisted.spread import pb, jelly
from twisted.internet import reactor

class test:
  def __init__(self):
    x = 3.
    y = 4.

class Bag:
  def setContent(self,content):
    self.content = content
  def printContent(self):
    print "My data is", self.content

class CopyBag(Bag, pb.Copyable):

class Sender(pb.Root):
  def __init__(self, bag):
    self.bag = bag

  def remote_sendBag(self, remote):
    self.remote = remote
    d = remote.callRemote("takeBag", self.bag)

  def ok(self, response):
    print "info: %s" % response
    d = self.remote.callRemote("finishTransfer")
    return None

  def notOk(self, failure):
    if failure.type == jelly.InsecureJelly:
      print "error: InsecureJelly"
    elif failure.type == pb.PBConnectionLost:
      print "info: data transfer finished"
    print failure
    return None

def main():
  from pbsimple import CopyBag

  icae = iCAEconfig()
  bag = CopyBag()

  bag.setContent('simple data') # --> OK
  #bag.setContent(test())       # --> InsecureJelly
  sender = Sender(bag)

  factory = pb.PBServerFactory(sender)
  reactor.listenTCP(8789, factory)

if __name__ == '__main__':


------------  pbsimleclient.py  -----------------

from twisted.spread import pb
from twisted.internet import reactor
from twisted.python import util

from pbsimple import Bag, CopyBag

class ReceiverBag(Bag,pb.RemoteCopy):

pb.setUnjellyableForClass(CopyBag, ReceiverBag)

class Receiver(pb.Referenceable):
  def remote_takeBag(self, bag):
    self.bag = bag
    return "data arrived safe and sound" # positive acknowledgement

  def remote_finishTransfer(self):
    print "got bag:", self.bag

  def requestBag(self,remote):
    print "asking server for sending the data bag"

def getContentFromServer():

  receiver = Receiver()
  factory = pb.PBClientFactory()
  reactor.connectTCP("localhost", 8789, factory,30)
  d = factory.getRootObject()
  return receiver.bag.content

if __name__ == '__main__':
  print getContentFromServer()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20061018/185592e7/attachment.htm 

More information about the Twisted-Python mailing list