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 DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent' | |
13 | |
14 | |
15 class Handler(BaseHTTPRequestHandler): | |
16 def get_header_values(self): | |
mathias
2017/08/08 07:51:46
Why all these string operations when a simple `re.
Vasily Kuznetsov
2017/08/08 11:30:51
This particular regexp doesn't quite work, I think
f.lopez
2017/08/09 19:50:47
Done.
| |
17 values = {} | |
18 for var in self.log_format.split(): | |
19 name = var.strip(PUNCTUATION) | |
20 if name.startswith(PROXY_TOKEN): | |
21 new_var = name[len(PROXY_TOKEN):].title() | |
22 values[name] = self.headers.get(new_var, '-') | |
23 return values | |
24 | |
25 def log_info(self, args): | |
mathias
2017/08/08 07:51:46
While the mimeo output looks like an HTTPd log, it
f.lopez
2017/08/09 19:50:48
Done.
| |
26 message = Template(self.log_format) | |
27 logging.info(message.safe_substitute(args)) | |
28 | |
29 def do_POST(self): | |
30 status = 200 | |
31 content = bytes(self.response, 'UTF-8') | |
32 values = { | |
33 'remote_addr': self.address_string(), | |
34 'time_local': self.log_date_time_string(), | |
35 'request': self.requestline, | |
36 'status': status, | |
37 'bytes_sent': len(content), | |
38 'content_type': 'text/plain', | |
39 'content_length': len(content), | |
40 } | |
41 values.update(self.get_header_values()) | |
42 self.log_info(values) | |
43 self.send_response(status) | |
44 self.send_header('Content-Type', values['content_type']) | |
45 self.send_header('Content-Length', values['content_length']) | |
46 self.end_headers() | |
47 self.wfile.write(content) | |
48 self.log_request(200) | |
mathias
2017/08/08 07:51:46
Shouldn't this use the status variable from above
f.lopez
2017/08/09 19:50:47
Done.
| |
49 | |
50 | |
51 if __name__ == '__main__': | |
52 parser = argparse.ArgumentParser() | |
53 parser.add_argument('--port', action='store', | |
54 default=8000, type=int, | |
55 nargs='?', | |
56 help='Port to use [default: 8000]') | |
57 parser.add_argument('--response', action='store', | |
58 type=str, nargs='?', default='OK', | |
59 help='The response send to the client') | |
60 parser.add_argument('--log_format', action='store', | |
61 type=str, nargs='?', | |
62 default=DEFAULT_LOG, | |
63 help='Format of the log ouput') | |
64 parser.add_argument('log_file', action='store', | |
65 type=str, nargs='?', | |
66 help='The file where the logs will be written') | |
67 args = parser.parse_args() | |
68 handler_class = Handler | |
69 server_class = HTTPServer | |
mathias
2017/08/08 07:51:45
Why the aliasing, additional variables?
f.lopez
2017/08/09 19:50:47
Done.
| |
70 setattr(handler_class, 'log_format', args.log_format) | |
mathias
2017/08/08 07:51:45
Analogous to log_info/write_info abobve, this prop
f.lopez
2017/08/09 19:50:47
Done.
| |
71 setattr(handler_class, 'response', args.response) | |
mathias
2017/08/08 07:51:45
Why invocations of setattr() instead of assignment
f.lopez
2017/08/09 19:50:48
The HTTPServer (server_class) receives a server ad
Vasily Kuznetsov
2017/08/14 10:59:55
I'm not sure what you mean here. Setting attribute
| |
72 server_address = ('', args.port) | |
73 httpd = server_class(server_address, handler_class) | |
74 if args.log_file is None: | |
75 logging.basicConfig(stream=sys.stdout, level=logging.INFO) | |
76 else: | |
77 logging.basicConfig(filename=args.log_file, level=logging.INFO) | |
78 httpd.serve_forever() | |
OLD | NEW |