Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: modules/adblockplus/files/mimeo.py

Issue 29504594: #2687 - Include mimeo python module (Closed)
Patch Set: For comments 21 and 24 Created Aug. 13, 2017, 4:13 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « hiera/roles/web/adblockbrowser.yaml ('k') | modules/adblockplus/manifests/web/mimeo.pp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python3
2
3 import argparse
4 import re
5 import sys
6 import threading
7
8 from http.server import BaseHTTPRequestHandler, HTTPServer
9 from string import Template
10
11 # The token used for the headers passed by nginx is: http_
12 REGEX = '\\$http_[a-z_0-9]+\\b'
Vasily Kuznetsov 2017/08/14 10:59:56 It's a bit more elegant to use a raw string here:
13 DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent'
14 _lock = threading.Lock()
15
16
17 class Handler(BaseHTTPRequestHandler):
18 def get_header_values(self):
19 values = {}
20 headers = re.findall(REGEX, self.format)
21 for name in headers:
22 new_var = name[6:]
23 values[name[1:]] = self.headers.get(new_var, '-')
24 return values
25
26 def write_info(self, args):
27 _lock.acquire()
28 try:
29 message = Template(self.format)
30 self.output.write(message.safe_substitute(args))
31 self.output.flush()
32 except Exception as e:
33 sys.stderr.write(e)
Vasily Kuznetsov 2017/08/14 10:59:56 I've just tested this and it seems to raise a Type
34 finally:
35 _lock.release()
36
37 def do_POST(self):
38 status = 200
39 content = bytes(self.response, 'UTF-8')
40 values = {
41 'remote_addr': self.address_string(),
42 'time_local': self.log_date_time_string(),
43 'request': self.requestline,
44 'status': status,
45 'bytes_sent': len(content),
46 }
47 values.update(self.get_header_values())
48 self.write_info(values)
49 self.send_response(status)
50 self.end_headers()
51 self.wfile.write(content)
52
53
54 if __name__ == '__main__':
55 parser = argparse.ArgumentParser()
56 parser.add_argument('--port', action='store',
57 default=8000, type=int,
58 nargs='?',
59 help='Port to use [default: 8000]')
60 parser.add_argument('--response', action='store',
61 type=str, nargs='?', default='OK',
62 help='The response send to the client')
63 parser.add_argument('--format', action='store',
64 type=str, nargs='?',
65 default=DEFAULT_LOG,
66 help='Format of the log ouput')
67 parser.add_argument('output', action='store',
68 type=str, nargs='?', default='-',
69 help='The file where the logs will be written')
70 args = parser.parse_args()
71 if args.output and args.output != '-':
72 fh = open(args.output, 'a')
73 else:
74 fh = sys.stdout
75 setattr(Handler, 'output', fh)
76 setattr(Handler, 'format', args.format)
77 setattr(Handler, 'response', args.response)
78 server_address = ('', args.port)
79 httpd = HTTPServer(server_address, Handler)
80 try:
81 httpd.serve_forever()
82 except:
83 if args.output != '-':
84 fh.close()
OLDNEW
« no previous file with comments | « hiera/roles/web/adblockbrowser.yaml ('k') | modules/adblockplus/manifests/web/mimeo.pp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld