Reputation: 398
I have a question that bothers me for a long time, currently I'm working on a daemon which operating requests from outside. Its like a server-client. In this project, I'm using a Twisted Python Framework, and I successful build a non-multithreading server, it works! But now, I need to serve several customers at once. I don't know how can I do this in Twisted Framework. I tried all what I know...
Please help me with this :|
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()
Upvotes: 3
Views: 1277
Reputation: 48315
Your server handles concurrent clients by default - without threads - as a property of the programming model followed by Twisted.
I don't see anything in your example code that invalidates this - if you remove the (broken) attempt to use threads this server shouldn't have any problem servicing more than one client.
Upvotes: 1