Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. from twisted.internet.protocol import Factory
  2. from twisted.internet import reactor, protocol
  3. from twisted.web.server import Site
  4. from twisted.web.wsgi import WSGIResource
  5. from flask import Flask
  6. app = Flask(__name__)
  7.  
  8.  
  9. @app.route('/')
  10. def hello():
  11. return 'Hello World!!! A Flask WSGI under Twisted/Tornado/Built-in development server...'
  12.  
  13. class CommandProtocol(protocol.Protocol):
  14. def __init__(self, factory, clients):
  15. self.factory = factory
  16. self.clients = clients
  17.  
  18. def connectionMade(self):
  19. self.clients.append(self)
  20. # self.clients.append(self.transport.getPeer())
  21.  
  22. def dataReceived(self, data):
  23. print "Number of active connections: {}".format(self.clients)
  24. print "> Received: > {}".format(data)
  25. self.send_command()
  26.  
  27. def send_command(self):
  28. client = raw_input('Enter client: ')
  29. command = raw_input('Enter command: ')
  30. self.clients[int(client)].transport.write(command)
  31.  
  32. def connectionLost(self, reason):
  33. self.clients.remove(self)
  34.  
  35. class CommandFactory(Factory):
  36.  
  37.  
  38. def __init__(self):
  39. self.clients = []
  40.  
  41. def buildProtocol(self, addr):
  42. return CommandProtocol(self, self.clients)
  43.  
  44. factory = CommandFactory()
  45. reactor.listenTCP(8001, factory)
  46. resource = WSGIResource(reactor, reactor.getThreadPool(), app)
  47. site = Site(resource)
  48.  
  49. reactor.listenTCP(8080, site, interface="0.0.0.0")
  50. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement