我有一个困扰我很长时间的问题,目前我正在研究一个从外部操作请求的守护进程。它就像一个服务器客户端。在这个项目中,我使用的是 Twisted Python 框架,并且我成功构建了一个非多线程服务器,它可以工作!但是现在,我需要同时为多个客户提供服务。我不知道如何在 Twisted Framework 中做到这一点。我尝试了所有我知道的...
请帮我解决这个问题:|
class Server(protocol.Protocol, protocol.Factory):
def buildProtocol(self, addr):
if addr.host in ipList:
log.msg("Connected from IP: " + addr.host)
return self
return None
def dataReceived(self, data):
reactor.callFromThread(self.actionCreator(data))
def actionCreator(self, data):
jsonData = json.loads(data)
if not jsonData["action"]:
log.msg("Incorrect data from IP: " + self.transport.getPeer().host + " data: " + data)
self.transport.write(json.dumps({'response' : '300'}))
elif jsonData["action"] == 'echo':
log.msg("Doing ask from IP: " + self.transport.getPeer().host)
self.transport.write(json.dumps({'response' : '400', 'data' : {'status' : 'online'}}))
elif jsonData["action"] == 'wget':
log.msg("Downloading file... for user - " + jsonData["user"])
os.system("wget -q http://****************")
self.transport.write(json.dumps({'response' : '400', 'data' : {'status' : 'downloaded'}}))
else:
log.msg("Incorrect data from IP: " + self.transport.getPeer().host + " data: " + data)
self.transport.write(json.dumps({'response' : '300'}))
if __name__ == '__main__':
try:
log.msg("Running server...")
context = ssl.DefaultOpenSSLContextFactory(certificate + "/server.key", certificate + "/server.crt")
log.msg("Initiating a secure SSL connection...")
reactor.listenSSL(config.getint(general, 'port'), Server(), context)
log.msg("Waiting for connections...")
reactor.run()
except KeyboardInterrupt:
sys.exit()