请您帮忙...。我真的很挣扎。我无法通过下面的程序从客户端调用服务器中的这两个功能。帮助我了解如何从客户端调用服务器中的多个功能。预先感谢
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()
如果我尝试执行此操作,则只执行remote_name函数。remote_hello函数没有响应。这是我的问题。
答案 0 :(得分:0)
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()
或
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()