forked from tariqbuilds/linux-dash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython-server.py
executable file
·54 lines (46 loc) · 1.86 KB
/
python-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
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer, test as _test
import subprocess
from SocketServer import ThreadingMixIn
import argparse
parser = argparse.ArgumentParser(description='Simple Threaded HTTP server to run linux-dash.')
parser.add_argument('--port', metavar='PORT', type=int, nargs='?', default=80,
help='Port to run the server on.')
modulesSubPath = '/server/modules/shell_files/'
serverPath = os.path.dirname(os.path.realpath(__file__))
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
class MainHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
data = ''
contentType = 'text/html'
if self.path.startswith("/server/"):
module = self.path.split('=')[1]
output = subprocess.Popen(
serverPath + modulesSubPath + module + '.sh',
shell = True,
stdout = subprocess.PIPE)
data = output.communicate()[0]
else:
if self.path == '/':
self.path = 'index.html'
f = open(os.path.dirname(os.path.realpath(__file__)) + os.sep + self.path)
data = f.read()
if self.path.startswith('/css/'):
contentType = 'text/css'
f.close()
self.send_response(200)
self.send_header('Content-type', contentType)
self.end_headers()
self.wfile.write(data)
except IOError:
self.send_error(404, 'File Not Found: %s' % self.path)
if __name__ == '__main__':
args = parser.parse_args()
server = ThreadedHTTPServer(('0.0.0.0', args.port), MainHandler)
print('Starting server, use <Ctrl-C> to stop')
server.serve_forever()