简体   繁体   中英

Python Twisted in PB, server client function invoking

Could you please help....I'm really struggling. I can't invoke both functions in server from the client side by below program.Help me to understand how to invoke multiple functions in server from client.Thanks in advance

Server.py

#!/usr/bin/env python
from twisted.spread import pb
from twisted.internet import reactor

class Main(pb.Root):
    def remote_name(self,idno):
            print'Id NO:', idno
            def num(idno):
                    switcher={
                            1:'\t\tFirst',
                            2:'\t\tSecond',
                            3:'\t\tThird',
                            } 
                    return switcher.get(idno, 'Invalid Number')
            print num(idno)
    def remote_hello(self, st):
            print'From Client:', st
            return st    
if __name__=='__main__':
      reactor.listenTCP(5000,pb.PBServerFactory(Main()))
      reactor.run()

Client.py

#!/usr/bin/env python

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

 def main():
      f = pb.PBClientFactory()
      reactor.connectTCP('localhost',5000,f)
      d=f.getRootObject()
      print 'Enter ID No:'
      no=input()
      d.addCallback(lambda new:new.callRemote('name',no))
      d.addCallback(lambda obj:obj.callRemote('hello', 'hello world'))
      reactor.run()

if __name__=='__main__':
      main()

If i try to execute this, remote_name function only executed.There is no response from remote_hello function.This is my problem.

  d = f.getRootObject()
  def gotObject(new):
      no = input()
      d2 = new.callRemote('name',no)
      d2.addCallback(lambda ignored: new.callRemote('hello', 'hello world'))
      return d2
  d.addCallback(gotObject)
  reactor.run()

Or

from twisted.internet.defer import inlineCallbacks

@inlineCallbacks
def doIt():
    f = pb.PBClientFactory()
    reactor.connectTCP('localhost',5000,f)
    new = yield f.getRootObject()
    print 'Enter ID No:'
    no = input()
    yield new.callRemote('name',no)
    yield new.callRemote('hello', 'hello world')

def main():
    d = doIt()
    reactor.run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM