Anders and Briegel in Python
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/python
  2. """
  3. This is a simple websocket relay server that facilitates realtime visualization of GraphStates.
  4. It doesn't do much except echo websocket messages to all connected clients, and serve some static content over HTTP.
  5. """
  6. from websocket_server import WebsocketServer
  7. from SimpleHTTPServer import SimpleHTTPRequestHandler
  8. from BaseHTTPServer import HTTPServer
  9. from SocketServer import ThreadingMixIn
  10. import os, sys, threading
  11. import webbrowser
  12. import argparse
  13. import abp
  14. import json
  15. from pkg_resources import resource_filename
  16. clients = []
  17. def new_message(client, server, message):
  18. if message.startswith("edit"):
  19. edit = json.loads(message[5:])
  20. print "Received update from javascript."
  21. print edit
  22. else:
  23. print "Received update from python {}.".format(client["id"])
  24. server.send_message_to_all(message)
  25. def new_client(client, server):
  26. print "Client {} connected.".format(client["id"])
  27. clients.append(client)
  28. def client_left(client, server):
  29. print "Client {} disconnected.".format(client["id"])
  30. clients.remove(client)
  31. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  32. """ Handle requests in a separate thread """
  33. if __name__ == '__main__':
  34. parser = argparse.ArgumentParser(description = "ABP websocket server")
  35. parser.add_argument("-v", action="store_true", help="Launch browser")
  36. args = parser.parse_args()
  37. # Change to the right working dir
  38. where = os.path.abspath(resource_filename("abp.static", ""))
  39. os.chdir(where)
  40. # Start the HTTP server
  41. httpserver = ThreadedHTTPServer(('', 5001), SimpleHTTPRequestHandler)
  42. thread = threading.Thread(target = httpserver.serve_forever)
  43. thread.daemon = True
  44. thread.start()
  45. if args.v:
  46. webbrowser.open("http://localhost:5001/")
  47. # Start the websocket server
  48. server = WebsocketServer(5000)
  49. server.set_fn_new_client(new_client)
  50. server.set_fn_message_received(new_message)
  51. server.set_fn_client_left(client_left)
  52. server.run_forever()
  53. httpserver.shutdown()