-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
41 lines (32 loc) · 1.76 KB
/
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
from http.server import BaseHTTPRequestHandler, HTTPServer
import socketserver
import subprocess
import json
import daemon
class MyHttpRequestHandler(BaseHTTPRequestHandler):
def _set_headers(self):
# create and send headers
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
def do_HEAD(self):
self._set_headers()
def do_GET(self):
self._set_headers()
# get resuls
res = subprocess.check_output(["status", "-h", "mobile", "1"])
res = res.decode('utf-8').strip()
res = dict([ [ item.strip() for item in ttuple.split(":")] for ttuple in res.split("\n")])
# return json
self.wfile.write(json.dumps(res).encode())
if __name__ == "__main__":
from sys import argv
port = 50000
server_address = ('', port)
server = HTTPServer(server_address, MyHttpRequestHandler)
# Make the context manager for becoming a daemon process.
daemon_context = daemon.DaemonContext()
daemon_context.files_preserve = [server.fileno()]
# Become a daemon process.
with daemon_context:
server.serve_forever()