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
57
58
59
60
61
62
63
64
65
|
import json, threading, Queue
import requests
import pathod, utils
import tutils
IFACE = "127.0.0.1"
class Daemon:
def __init__(self, staticdir=None, anchors=(), ssl=None):
self.q = Queue.Queue()
self.thread = PaThread(self.q, staticdir, anchors, ssl)
self.thread.start()
self.port = self.q.get(True, 5)
self.urlbase = "%s://%s:%s"%("https" if ssl else "http", IFACE, self.port)
def info(self):
"""
Return some basic info about the remote daemon.
"""
resp = requests.get("%s/api/info"%self.urlbase, verify=False)
return resp.json
def log(self):
"""
Return the log buffer as a list of dictionaries.
"""
resp = requests.get("%s/api/log"%self.urlbase, verify=False)
return resp.json["log"]
def clear_log(self):
"""
Clear the log.
"""
resp = requests.get("%s/api/clear_log"%self.urlbase, verify=False)
return resp.ok
def shutdown(self):
"""
Shut the daemon down, return after the thread has exited.
"""
self.thread.server.shutdown()
self.thread.join()
class PaThread(threading.Thread):
def __init__(self, q, staticdir, anchors, ssl):
threading.Thread.__init__(self)
self.q, self.staticdir, self.anchors, self.ssl = q, staticdir, anchors, ssl
def run(self):
if self.ssl is True:
ssloptions = dict(
keyfile = utils.data.path("resources/server.key"),
certfile = utils.data.path("resources/server.crt"),
)
else:
ssloptions = self.ssl
self.server = pathod.Pathod(
(IFACE, 0),
ssloptions = ssloptions,
anchors = self.anchors,
staticdir = self.staticdir
)
self.q.put(self.server.port)
self.server.serve_forever()
|