我正在尝试创建一个与本地代码库(客户端部分)通信的扭曲守护进程(服务器部分)。基本上,客户端应该使用 AMP 调用远程()到守护程序以开始一些处理(更新数据库)一些方法。在服务器上完成每个方法的处理后,我需要服务器调用远程()到我的客户端,以便用户知道服务器的进度。
我已经能够从客户端调用服务器并获得响应,但我无法让服务器向客户端发送响应。
我搜索了一个解决方案,但找不到任何使用 AMP 进行双向通信的示例代码——它始终是客户端调用服务器。
我试图让客户端调用服务器以开始处理(ServerStart AMP 命令),然后让服务器将多个调用发送回客户端以提供处理更新(MessageClient AMP 命令)。
任何帮助将不胜感激。一个超级简单的示例展示了如何从客户端调用服务器,然后让服务器将两个调用传回给客户端,这将是非常棒的!
ampclient.py
from client_server import MessageServer, Client, ServerStart
from twisted.internet.protocol import ClientCreator
from twisted.internet import reactor
from twisted.protocols import amp
from time import sleep
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.application.service import Application
from twisted.application.internet import StreamServerEndpointService
def startServerProcess():
def show_start(result):
print 'result from server: %r' % result
d = ClientCreator(reactor, amp.AMP).connectTCP(
'127.0.0.1', 1234).addCallback(
lambda p: p.callRemote(ServerStart, truncate=True)).addCallback(
show_start)
pf = Factory()
pf.protocol = Client
reactor.listenTCP(1235, pf)
print 'client listening'
startServerProcess()
sleep(4)
reactor.run()
ampserver.py
from client_server import MessageClient, Server
from twisted.internet.protocol import ClientCreator
from twisted.internet import reactor
from twisted.protocols import amp
from time import sleep
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.application.service import Application
from twisted.application.internet import StreamServerEndpointService
def makeClientCall():
def show_result(result):
print 'result from client: %r' % result
d = ClientCreator(reactor, amp.AMP).connectTCP(
'127.0.0.1', 1235).addCallback(
lambda p: p.callRemote(MessageClient)).addCallback(
show_result)
application = Application("server app")
endpoint = TCP4ServerEndpoint(reactor, 1234)
factory = Factory()
factory.protocol = Server
service = StreamServerEndpointService(endpoint, factory)
service.setServiceParent(application)
sleep(4)
makeClientCall()
makeClientCall()
客户端服务器.py
from twisted.protocols import amp
from twisted.internet import reactor
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.application.service import Application
from twisted.application.internet import StreamServerEndpointService
class MessageServer(amp.Command):
response = [('msg', amp.String())]
class ServerStart(amp.Command):
arguments = [('truncate', amp.Boolean())]
response = [('msg', amp.String())]
class Server(amp.AMP):
def message_it(self):
msg = 'This is a message from the server'
print 'msg sent to client: %s' % msg
return {'msg': msg}
MessageServer.responder(message_it)
def start_it(self, truncate):
msg = 'Starting processing...'
return {'msg': msg}
ServerStart.responder(start_it)
class MessageClient(amp.Command):
response = [('msg', amp.String())]
class Client(amp.AMP):
def message_it(self):
msg = 'This is a message from the client'
return {'msg': msg}
MessageClient.responder(message_it)