Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python3 | |
2 | |
3 import argparse | |
4 import logging | |
5 import sys | |
6 | |
7 from http.server import BaseHTTPRequestHandler, HTTPServer | |
8 from string import Template | |
9 | |
10 PUNCTUATION = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ """ | |
11 PROXY_TOKEN = 'http_' | |
12 | |
13 | |
14 class Handler(BaseHTTPRequestHandler): | |
15 def get_header_values(self): | |
16 values = {} | |
17 for var in self.log_format.split(): | |
18 name = var.strip(PUNCTUATION) | |
Vasily Kuznetsov
2017/08/07 20:46:01
You've added a space to PUNCTUATION above, but do
f.lopez
2017/08/08 02:48:17
Done.
| |
19 if name.startswith(PROXY_TOKEN): | |
20 new_var = name[len(PROXY_TOKEN):].title() | |
21 values[name] = self.headers.get(new_var, '-') | |
22 return values | |
23 | |
24 def log_info(self, args): | |
25 message = Template(self.log_format) | |
26 logging.info(message.safe_substitute(args)) | |
27 | |
28 def do_POST(self): | |
29 status = 200 | |
30 content = bytes(self.response, 'UTF-8') | |
31 values = { | |
32 'remote_addr': self.address_string(), | |
33 'time_local': self.log_date_time_string(), | |
34 'request': self.requestline, | |
35 'status': status, | |
36 'bytes_sent': len(content), | |
37 'content-type': 'text/plain', | |
Vasily Kuznetsov
2017/08/07 20:46:01
Some other variables, like 'time_local' and 'bytes
f.lopez
2017/08/08 02:48:17
Done.
| |
38 'content-length': len(content), | |
39 } | |
40 values.update(self.get_header_values()) | |
41 self.log_info(values) | |
42 self.send_response(status) | |
43 self.send_header('Content-Type', values['content-type']) | |
44 self.send_header('Content-Length', values['content-length']) | |
45 self.end_headers() | |
46 self.wfile.write(content) | |
47 self.log_request(200) | |
48 | |
49 | |
50 if __name__ == '__main__': | |
51 parser = argparse.ArgumentParser() | |
52 parser.add_argument('--port', action='store', | |
53 default=8000, type=int, | |
54 nargs='?', | |
55 help='Port to use [default: 8000]') | |
56 parser.add_argument('--response', action='store', | |
57 type=str, nargs='?', default='OK', | |
58 help='The response send to the client') | |
59 parser.add_argument('--log_format', action='store', | |
60 type=str, nargs='?', | |
61 default='$remote_addr - - [$time_local] "$request" $stat us $bytes_sent', | |
Vasily Kuznetsov
2017/08/07 20:46:01
This line is too long now, PEP8 (and our style gui
f.lopez
2017/08/08 02:48:17
Done.
| |
62 help='Format of the log ouput') | |
63 parser.add_argument('log_file', action='store', | |
64 type=str, nargs='?', | |
65 help='The file where the logs will be written') | |
66 args = parser.parse_args() | |
67 handler_class = Handler | |
68 server_class = HTTPServer | |
69 setattr(handler_class, 'log_format', args.log_format) | |
70 setattr(handler_class, 'response', args.response) | |
71 server_address = ('', args.port) | |
72 httpd = server_class(server_address, handler_class) | |
73 if args.log_file == None: | |
74 logging.basicConfig(stream=sys.stdout, level=logging.INFO) | |
75 else: | |
76 logging.basicConfig(filename=args.log_file, level=logging.INFO) | |
77 httpd.serve_forever() | |
OLD | NEW |