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

Franz Zieher franz.zieher at gmail.com
Tue Oct 17 18:05:28 MDT 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

Franz

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):
  pass

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)
    d.addCallback(self.ok).addErrback(self.notOk)

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

  def notOk(self, failure):
    if failure.type == jelly.InsecureJelly:
      print "error: InsecureJelly"
    elif failure.type == pb.PBConnectionLost:
      print "info: data transfer finished"
    else:
    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)
  reactor.run()

if __name__ == '__main__':
  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):
  pass

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
    reactor.stop()

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

def getContentFromServer():

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

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

---------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20061018/185592e7/attachment.html>


More information about the Twisted-Python mailing list