Anders and Briegel in Python
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

55 linhas
1.6KB

  1. #!/usr/bin/python
  2. from websocket_server import WebsocketServer
  3. from SimpleHTTPServer import SimpleHTTPRequestHandler
  4. from BaseHTTPServer import HTTPServer
  5. from SocketServer import ThreadingMixIn
  6. import os, sys, threading
  7. import webbrowser
  8. import argparse
  9. import abp
  10. clients = []
  11. def new_message(client, server, message):
  12. print "Received update from client {}.".format(client["id"])
  13. server.send_message_to_all(message)
  14. def new_client(client, server):
  15. print "Client {} connected.".format(client["id"])
  16. clients.append(client)
  17. def client_left(client, server):
  18. print "Client {} disconnected.".format(client["id"])
  19. clients.remove(client)
  20. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  21. """ Handle requests in a separate thread """
  22. if __name__ == '__main__':
  23. parser = argparse.ArgumentParser(description = "ABP websocket server")
  24. parser.add_argument("-v", action="store_false", help="Launch browser")
  25. args = parser.parse_args()
  26. # Change to the right working dir
  27. where = os.path.join(os.path.dirname(abp.__path__[0]), "static")
  28. os.chdir(where)
  29. # Start the HTTP server
  30. httpserver = ThreadedHTTPServer(('', 5001), SimpleHTTPRequestHandler)
  31. thread = threading.Thread(target = httpserver.serve_forever)
  32. thread.daemon = True
  33. thread.start()
  34. if args.v:
  35. webbrowser.open("http://localhost:5001/")
  36. # Start the websocket server
  37. server = WebsocketServer(5000)
  38. server.set_fn_new_client(new_client)
  39. server.set_fn_message_received(new_message)
  40. server.set_fn_client_left(client_left)
  41. server.run_forever()
  42. httpserver.shutdown()