-
Notifications
You must be signed in to change notification settings - Fork 42
/
Command2Api_Py3.py
50 lines (42 loc) · 1.2 KB
/
Command2Api_Py3.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
import sys
import uuid
import threading
import subprocess
from http.server import HTTPServer, SimpleHTTPRequestHandler
screen_list = []
uri = '/' + str(uuid.uuid4()).replace("-", "")
class NewThread(threading.Thread):
def __init__(self, thread_name, command):
threading.Thread.__init__(self, name='Thread_' + thread_name)
self.thread_name = int(thread_name)
self.command = command
def run(self):
global screen_list
ret = subprocess.Popen(
self.command,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
for i in iter(ret.stdout.readline, b""):
res = i.decode().strip()
print(res)
screen_list.append(res)
class ServerHandler(SimpleHTTPRequestHandler):
def do_GET(self):
global screen_list
if self.path == uri:
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
self.wfile.write("\n".join(screen_list).encode())
if __name__ == '__main__':
# New Thread: Get Command Result
t1 = NewThread('1', sys.argv[1])
t1.start()
# Webserver
port = int(sys.argv[2])
print("URL: http://localhost:{0}{1}".format(port, uri))
httpd = HTTPServer(('0.0.0.0', port), ServerHandler)
httpd.serve_forever()