-
Notifications
You must be signed in to change notification settings - Fork 0
/
httop.py
57 lines (50 loc) · 1.9 KB
/
httop.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
# coding=utf-8
import subprocess
import string
import BaseHTTPServer
import unicodedata
import parsers
HOST_NAME = "localhost" #'bandini.hi.inet'
PORT_NUMBER = 8080
#Config Commands for
#USED IN parsers.parse_top(output_command, header_separator=False, include_header=False)
class MyDict(dict):
#noinspection PyUnusedLocal
def __missing__(self, key):
return False
requestDispatcher = MyDict({
'/ls': MyDict({'path': "ls -la $HOME", 'parse': parsers.parse}),
'/top': MyDict({'path': "top -l 1", 'parse': parsers.parse, 'header_separator': ""}),
'/top/basic': MyDict({'path': "ls -la $HOME", 'parse': parsers.parse,
'header_separator': 'drwxr-xr-x 2 mru mru 68 May 17 2010 .Xcode',
'include_header': True})
})
def do_command(command, shell_flag=True):
output_command = subprocess.check_output(command['path'], shell=shell_flag)
html = command['parse'](output_command, command['header_separator'], command['include_header'])
return html
class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
global requestDispatcher
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
#manage request
path = string.lower(self.path)
try:
command = requestDispatcher[path]
except KeyError :
html = "Unsupported Command"
self.wfile.write(html)
else:
if command:
html = do_command(command) #Hechizo nivel Archimago
html = unicodedata.normalize('NFKD', html).encode('ascii', 'ignore')
self.wfile.write(html)
#Start the Http Server
http_server = server_class = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER), HTTPHandler)
try:
http_server.serve_forever()
except KeyboardInterrupt:
pass
http_server.server_close()