[英]Python Twisted in PB, server client function invoking
你能幫忙嗎?...我真的很掙扎。 我無法通過下面的程序從客戶端調用服務器中的這兩個功能。幫助我了解如何從客戶端調用服務器中的多個功能。
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()
客戶端
#!/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函數沒有響應。這是我的問題。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.