Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 import cherrypy | |
|
anandc
2014/03/03 21:10:03
Would the BaseHttpServer module that comes with Py
anandc
2014/03/03 21:36:37
Just noticed that the link doesn't work. This does
chaitali
2014/03/04 19:33:45
Not for our purposes. Its pretty hard to code path
| |
| 2 import json | |
| 3 import os | |
| 4 | |
| 5 | |
|
anandc
2014/03/03 21:10:03
Please run gpylint on this module. That should ide
chaitali
2014/03/04 19:33:45
Done on most things. It still complains about inva
| |
| 6 def http_methods_allowed(methods=['GET', 'HEAD']): | |
| 7 method = cherrypy.request.method.upper() | |
| 8 if method not in methods: | |
| 9 cherrypy.response.headers['Allow'] = ", ".join(methods) | |
| 10 raise cherrypy.HTTPError(405) | |
| 11 | |
| 12 cherrypy.tools.allow = cherrypy.Tool('on_start_resource', http_methods_allowed) | |
| 13 | |
| 14 class KeyTest(object): | |
| 15 keytest_succeeded = False | |
| 16 keytest_text = None | |
| 17 | |
| 18 @cherrypy.expose | |
| 19 @cherrypy.tools.allow(methods=['POST']) | |
| 20 def test(self, text): | |
| 21 self.keytest_succeeded = True | |
| 22 self.keytest_text = text | |
| 23 | |
| 24 def process(self): | |
| 25 # Build the message that will be conveyed to client | |
| 26 message = {} | |
| 27 message['keypressSucceeded'] = self.keytest_succeeded | |
| 28 message['keypressText'] = self.keytest_text | |
| 29 | |
| 30 # The message is now built so reset state on the server | |
| 31 if self.keytest_succeeded: | |
| 32 self.keytest_succeeded = False | |
| 33 self.keytest_text = None | |
| 34 | |
| 35 return message | |
| 36 | |
| 37 | |
| 38 class Root(object): | |
| 39 # Every test has its own class which should be instantiated here | |
| 40 keytest = KeyTest() | |
| 41 | |
| 42 # Every test's class should have a process method that the client polling | |
| 43 # will call when that test is running. | |
| 44 # The method should be registered here with the test name. | |
| 45 TEST_DICT = { | |
| 46 'keytest': keytest.process | |
| 47 } | |
| 48 | |
| 49 @cherrypy.expose | |
| 50 @cherrypy.tools.allow() | |
| 51 def index(self): | |
| 52 return "Simple HTTP Server for Chromoting Browser Tests!" | |
| 53 | |
| 54 @cherrypy.expose | |
| 55 @cherrypy.tools.allow() | |
| 56 def poll(self, test): | |
| 57 print test | |
| 58 if test not in self.TEST_DICT: | |
| 59 cherrypy.response.status = 500 | |
| 60 return | |
| 61 | |
| 62 cherrypy.response.headers['Content-Type'] = "application/json" | |
| 63 return json.dumps(self.TEST_DICT[test]()) | |
| 64 | |
| 65 | |
| 66 app_config = { | |
| 67 '/static':{ | |
| 68 'tools.staticdir.on': True, | |
| 69 'tools.staticdir.dir': | |
| 70 os.path.abspath(os.path.dirname(__file__)) | |
| 71 } | |
| 72 } | |
| 73 cherrypy.tree.mount(Root(), '/', config = app_config) | |
| 74 cherrypy.config.update({'server.socket_host': '0.0.0.0', | |
| 75 }) | |
| 76 cherrypy.engine.start() | |
| 77 cherrypy.engine.block() | |
| OLD | NEW |