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, log_format): | |
Vasily Kuznetsov
2017/08/07 13:06:41
What about not passing `log_format`? You can alway
f.lopez
2017/08/07 18:40:01
Done.
| |
16 values = {} | |
17 for var in log_format.split(): | |
18 name = var.strip() | |
Vasily Kuznetsov
2017/08/07 13:06:41
Shouldn't you remove punctuation here instead of s
f.lopez
2017/08/07 18:40:01
Done.
| |
19 if name.startswith(PROXY_TOKEN): | |
20 name = var.strip(PUNCTUATION) | |
21 new_var = name[len(PROXY_TOKEN):].title() | |
22 values[name] = self.headers.get(new_var, '-') | |
23 return values | |
24 | |
25 def message_log(self, form, args): | |
Vasily Kuznetsov
2017/08/07 13:06:40
This name is somewhat confusing, looks like a meth
f.lopez
2017/08/07 18:40:02
Done.
| |
26 message = Template(form) | |
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), | |
Vasily Kuznetsov
2017/08/07 13:06:40
Oops, double quote :)
f.lopez
2017/08/07 18:40:01
Done.
| |
38 'Content-Type': 'text/plain', | |
39 'Content-Length': len(content), | |
40 } | |
41 values.update(self.get_header_values(self.log_format)) | |
42 self.message_log(self.log_format, 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) | |
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='Specify alternate port [default: 8000]') | |
Vasily Kuznetsov
2017/08/07 13:06:40
I don't think you need "Specify" here either.
f.lopez
2017/08/07 18:40:02
Done.
| |
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', | |
Vasily Kuznetsov
2017/08/07 13:06:40
Probably having some kind of default log format wo
f.lopez
2017/08/07 18:40:01
Done.
| |
61 type=str, nargs='?', | |
62 help='Format of the log ouput') | |
63 parser.add_argument('log_file', action='store', | |
64 type=str, nargs='?', default=sys.stdout, | |
Vasily Kuznetsov
2017/08/07 13:06:40
Can you pass `sys.stdout` as `filename` to `loggin
f.lopez
2017/08/07 18:40:02
You are right, I got the error as well, I'm pretty
| |
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 logging.basicConfig(filename=args.log_file, level=logging.INFO) | |
74 httpd.serve_forever() | |
OLD | NEW |