-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_server.py
72 lines (60 loc) · 2.42 KB
/
http_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import logging
import os
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn, ForkingMixIn
import prometheus_client
import errno
class AssetsHandler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.serve_file(self.path, False)
def do_GET(self):
if self.path == '/':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('<html><body><p><a href="/metrics">Metrics</a></p></body></html>')
elif self.path == '/metrics':
self.send_response(200)
self.send_header('Content-Type', 'text/plain; version=0.0.4; charset=utf-8')
self.end_headers()
self.wfile.write(prometheus_client.generate_latest())
else:
self.serve_file(self.path, True)
def serve_file(self, path, send_body=False):
try:
if self.path.endswith('.mp3'):
f = open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'assets' + path))
f_content = f.read()
file_size = len(f_content)
f.close()
self.send_response(200)
self.send_header('Content-type', 'audio/mpeg')
self.send_header('Content-length', file_size)
self.end_headers()
if send_body:
self.wfile.write(f_content)
else:
self.send_error(404, 'File Not Found: %s' % path)
except IOError, e:
if e.errno == errno.ENOENT:
self.send_error(404, 'File Not Found: %s' % path)
else:
raise
def log_message(self, format, *args):
logging.info('Serve HTTP request: %s' % format%args)
def log_request(self, code='-', size='-'):
if self.path == '/metrics' and code == 200:
return
self.log_message("%s' %s %s", self.requestline, str(code), str(size))
class HttpServer(ForkingMixIn, HTTPServer):
def __init__(self, port):
HTTPServer.__init__(self, ('', port), AssetsHandler)
def finish_request(self, request, client_address):
try:
HTTPServer.finish_request(self, request, client_address)
except IOError, e:
if e.errno == errno.EPIPE:
# Silence broken pipe error.
pass
else:
raise