Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python3 | |
2 | |
3 import argparse | |
4 import re | |
5 import sys | |
6 import threading | |
7 import traceback | |
8 | |
9 from http.server import BaseHTTPRequestHandler, HTTPServer | |
10 from string import Template | |
11 | |
12 # The token used for the headers passed by nginx is: http_ | |
13 REGEX = r'\$http_\w+\b' | |
14 DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent' | |
15 _lock = threading.Lock() | |
16 | |
17 | |
18 class Handler(BaseHTTPRequestHandler): | |
19 def get_header_values(self): | |
20 values = {} | |
21 headers = re.findall(REGEX, self.format) | |
22 for name in headers: | |
23 new_var = name[6:] | |
24 values[name[1:]] = self.headers.get(new_var, '-') | |
25 return values | |
26 | |
27 def send_simple_response(self, status, response=None): | |
28 self.send_response(status) | |
29 self.end_headers() | |
30 if response is None: | |
31 response = bytes(self.responses[status][0], 'UTF-8') | |
32 self.wfile.write(response) | |
33 | |
34 def write_info(self, args): | |
35 message = Template(self.format) | |
36 with _lock: | |
37 self.output.write(message.safe_substitute(args) + '\n') | |
mathias
2017/08/19 15:06:08
The substitution and concatenation should also hap
f.lopez
2017/08/20 23:49:55
Done.
| |
38 self.output.flush() | |
39 | |
40 def do_POST(self): | |
41 status = 200 | |
42 content = bytes(self.response, 'UTF-8') | |
43 values = { | |
44 'remote_addr': self.address_string(), | |
45 'time_local': self.log_date_time_string(), | |
46 'request': self.requestline, | |
47 'status': status, | |
48 'bytes_sent': len(content), | |
49 } | |
50 values.update(self.get_header_values()) | |
51 try: | |
52 self.write_info(values) | |
53 self.send_simple_response(status, content) | |
54 except: | |
55 traceback.print_exc(file=sys.stderr) | |
56 self.send_simple_response(500) | |
57 | |
58 | |
59 if __name__ == '__main__': | |
60 parser = argparse.ArgumentParser() | |
61 parser.add_argument('--port', action='store', | |
62 default=8000, type=int, | |
63 nargs='?', | |
64 help='Port to use [default: 8000]') | |
65 parser.add_argument('--response', action='store', | |
66 type=str, nargs='?', default='OK', | |
67 help='The response send to the client') | |
68 parser.add_argument('--format', action='store', | |
69 type=str, nargs='?', | |
70 default=DEFAULT_LOG, | |
71 help='Format of the log ouput') | |
72 parser.add_argument('output', action='store', | |
73 type=str, nargs='?', default='-', | |
74 help='The file where the logs will be written') | |
75 try: | |
76 args = parser.parse_args() | |
77 if args.output and args.output != '-': | |
78 fh = open(args.output, 'a') | |
79 else: | |
80 fh = open(sys.stdout.fileno(), 'w', closefd=False) | |
81 Handler.output = fh | |
82 Handler.format = args.format | |
83 Handler.response = args.response | |
84 server_address = ('', args.port) | |
85 httpd = HTTPServer(server_address, Handler) | |
86 httpd.serve_forever() | |
87 except: | |
88 fh.close() | |
OLD | NEW |