/
ufwHost.py
executable file
·52 lines (45 loc) · 1.5 KB
/
ufwHost.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
"""
Simple python server that will execute commands sent from the mapService on the host.
"""
import time
import json
import subprocess
import requests
from urllib.parse import urlparse, parse_qs
from http.server import BaseHTTPRequestHandler, HTTPServer
HOST_NAME = "localhost"
PORT_NUMBER = 8080
class ufwHost(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_POST(self):
length = int(self.headers.get("content-length"))
field_data = self.rfile.read(length)
print(field_data)
self.send_response(200)
self.end_headers()
# TODO validate
cmd = field_data
print(cmd)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
p_status = p.wait()
(output, err) = p.communicate()
print(output)
print("Command exit status/return code : ", p_status)
self.wfile.write(cmd)
return
def respond(self, opts):
response = self.handle_http(opts["status"], self.path)
self.wfile.write(response)
if __name__ == "__main__":
server_class = HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), ufwHost)
print(time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER))