Python PB中的Twisted,服务端客户端函数调用
Python Twisted in PB, server client function invoking
你能帮忙吗....我真的很挣扎。我无法通过以下方式从客户端调用服务器中的两个函数 program.Help 我了解如何提前从 client.Thanks 调用服务器中的多个函数
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 函数只有 executed.There 没有来自 remote_hello 的响应 function.This 是我的问题。
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()
你能帮忙吗....我真的很挣扎。我无法通过以下方式从客户端调用服务器中的两个函数 program.Help 我了解如何提前从 client.Thanks 调用服务器中的多个函数
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 函数只有 executed.There 没有来自 remote_hello 的响应 function.This 是我的问题。
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()