Add twisted-15.5.0 module to webkitpy.thirdparty.autoinstalled.
[WebKit.git] / Tools / Scripts / webkitpy / benchmark_runner / http_server_driver / http_server / twisted_http_server.py
1 #!/usr/bin/env python
2
3 import argparse
4 import logging
5 import os
6 import sys
7
8 try:
9     import twisted
10 except ImportError:
11     sys.path.append(os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../..')))
12     from webkitpy.thirdparty.autoinstalled.twisted_15_5_0 import twisted
13
14 from twisted.web import static, server
15 from twisted.web.resource import Resource
16 from twisted.internet import reactor
17
18 _log = logging.getLogger(__name__)
19
20 class ServerControl(Resource):
21     isLeaf = True
22
23     def render_GET(self, request):
24         _log.info("Serving request %s" % request)
25         reactor.stop()
26         return ""
27
28     def render_POST(self, request):
29         _log.info("Serving request %s" % request)
30         sys.stdout.write(request.content.getvalue())
31         sys.stdout.flush()
32         return 'OK'
33
34
35 if __name__ == '__main__':
36     parser = argparse.ArgumentParser(description='python twisted_http_server.py web_root')
37     parser.add_argument('web_root')
38     parser.add_argument('--port', type=int, default=0)
39     args = parser.parse_args()
40     web_root = static.File(args.web_root)
41     serverControl = ServerControl()
42     web_root.putChild('shutdown', serverControl)
43     web_root.putChild('report', serverControl)
44     reactor.listenTCP(args.port, server.Site(web_root))
45     reactor.run()