1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#! /usr/bin/python
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""A simple http server for running local integration tests.
This chooses a port dynamically and so can communicate that back to its spawner
via a named pipe at --fifo. Sources are served from the tree named at
--source_dir.
"""
import argparse
import cgi
import os.path
import logging
import re
import time
import wsgiref.simple_server
class ServerApp(object):
"""WSGI App.
Dispatches by matching, in order, against GetPaths.
"""
def __init__(self, source_dir):
self._source_dir = source_dir
def __call__(self, environ, start_response):
"""WSGI dispatch.
Args:
environ: environment list.
start_response: WSGI response start.
Returns:
Iterable server result.
"""
path = environ.get('PATH_INFO', '')
while path.startswith('/'):
path = path[1:]
filename = os.path.join(self._source_dir, path)
if not os.path.exists(filename):
logging.info('%s not found', filename)
start_response('404 Not Found', [('Content-Type', 'text/html')])
return ["""<!DOCTYPE html>
<html>
<head>
<title>Not Found</title>
<body>%s not found</body>
</html>""" % path]
logging.info('responding with %s', filename)
suffix = path[path.rfind('.') + 1:]
start_response('200 OK', [('Content-Type',
{'css': 'text/css',
'html': 'text/html',
'jpg': 'image/jpeg',
'js': 'text/javascript',
'png': 'image/png',
'ttf': 'font/ttf',
}[suffix])])
return file(filename).read()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument('--fifo', default=None,
help='Named pipe used to communicate port')
parser.add_argument('--source_dir', required=True,
help='Directory holding sources to serve.')
args = parser.parse_args()
server_app = ServerApp(args.source_dir)
server = wsgiref.simple_server.make_server(
'localhost', 0, server_app)
ip, port = server.server_address
logging.info('Listening on port %s at %s', port, args.source_dir)
if args.fifo:
fifo = file(args.fifo, 'w')
fifo.write('%s\n' % port)
fifo.flush()
fifo.close()
server.serve_forever()
|