summaryrefslogtreecommitdiff
path: root/python/gras/stats/__init__.py
blob: ce570b07a00aaa24f8c8a907b5c1941cd90e4b98 (plain)
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
import time
import BaseHTTPServer

import os
__path__ = os.path.abspath(os.path.dirname(__file__))

get_stats_registry = [lambda: ""]

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_HEAD(s):
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()

    def do_GET(s):
        """Respond to a GET request."""
        if s.path.endswith('stats.xml'):
            s.send_response(200)
            s.send_header("Content-type", "text/xml")
            s.end_headers()
            s.wfile.write(get_stats_registry[0]())
            return
        path = s.path
        if path.startswith('/'): path = path[1:]
        if not path: path = 'main.html'
        target = os.path.join(__path__, path)
        if os.path.exists(target):
            s.send_response(200)
            s.send_header("Content-type", "text/html")
            s.end_headers()
            s.wfile.write(open(target).read())
        else:
            s.send_response(404)
            s.send_header("Content-type", "text/html")
            s.end_headers()
            s.wfile.write("<p>not found</p>")

import select

class http_server(object):
    def __init__(self, args, top_block):
        get_stats_registry[0] = top_block.get_stats_xml
        server_class = BaseHTTPServer.HTTPServer
        self._httpd = server_class(args, MyHandler)

    def serve_forever(self):
        while True:
            try: self._httpd.serve_forever()
            except select.error: pass