Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4KB

  1. import urlparse
  2. from BaseHTTPServer import BaseHTTPRequestHandler
  3. from SimpleHTTPServer import SimpleHTTPRequestHandler
  4. import SocketServer
  5. import sys, json, time, os
  6. from graphstate import GraphState
  7. import requests
  8. class VizHandler(SimpleHTTPRequestHandler):
  9. """ Handles requests to the server """
  10. def get_state(self):
  11. """ Get the current graphstate state """
  12. self.send_response(200)
  13. self.send_header('Content-Type', 'application/json')
  14. self.end_headers()
  15. self.wfile.write(state.to_json())
  16. def do_GET(self, *args, **kwargs):
  17. """ Someone belled the server """
  18. parsed_path = urlparse.urlparse(self.path)
  19. if parsed_path.path == "/state":
  20. return self.get_state()
  21. else:
  22. return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs)
  23. class Server(SocketServer.TCPServer):
  24. """ Serves the good stuff """
  25. allow_reuse_address = True
  26. def __init__(self, port=8000):
  27. self.port = port
  28. self.state = None
  29. SocketServer.TCPServer.__init__(self, ("127.0.0.1", self.port), VizHandler)
  30. print "Serving on port {} (Press CTRL-C to quit)".format(self.port)
  31. self.serve_forever()
  32. if __name__ == '__main__':
  33. os.chdir(os.path.join(sys.path[0], "../static"))
  34. server = Server()
  35. server.shutdown()
  36. requests.get("http://localhost:8000/state")