2014-10-24 01:01:34 +00:00
|
|
|
import threading
|
|
|
|
import Queue
|
2012-06-06 23:23:23 +00:00
|
|
|
import requests
|
2014-09-06 23:38:44 +00:00
|
|
|
import requests.packages.urllib3
|
2014-01-19 05:20:01 +00:00
|
|
|
import pathod
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2014-09-06 23:38:44 +00:00
|
|
|
requests.packages.urllib3.disable_warnings()
|
|
|
|
|
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
class Daemon:
|
2012-07-30 05:29:36 +00:00
|
|
|
IFACE = "127.0.0.1"
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2012-07-24 22:34:57 +00:00
|
|
|
def __init__(self, ssl=None, **daemonargs):
|
2012-06-06 23:23:23 +00:00
|
|
|
self.q = Queue.Queue()
|
2012-07-30 05:29:36 +00:00
|
|
|
self.thread = _PaThread(self.IFACE, self.q, ssl, daemonargs)
|
2012-06-06 23:23:23 +00:00
|
|
|
self.thread.start()
|
|
|
|
self.port = self.q.get(True, 5)
|
2015-05-30 00:03:13 +00:00
|
|
|
self.urlbase = "%s://%s:%s" % (
|
2014-10-24 01:01:34 +00:00
|
|
|
"https" if ssl else "http",
|
|
|
|
self.IFACE,
|
|
|
|
self.port
|
|
|
|
)
|
2012-07-30 05:29:36 +00:00
|
|
|
|
2012-07-30 08:58:59 +00:00
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
self.shutdown()
|
|
|
|
return False
|
|
|
|
|
2012-07-30 05:29:36 +00:00
|
|
|
def p(self, spec):
|
|
|
|
"""
|
|
|
|
Return a URL that will render the response in spec.
|
|
|
|
"""
|
2015-05-30 00:03:13 +00:00
|
|
|
return "%s/p/%s" % (self.urlbase, spec)
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2012-06-06 23:39:37 +00:00
|
|
|
def info(self):
|
2012-06-21 03:39:40 +00:00
|
|
|
"""
|
|
|
|
Return some basic info about the remote daemon.
|
|
|
|
"""
|
2015-05-30 00:03:13 +00:00
|
|
|
resp = requests.get("%s/api/info" % self.urlbase, verify=False)
|
2012-12-30 23:23:42 +00:00
|
|
|
return resp.json()
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2012-07-30 05:29:36 +00:00
|
|
|
def last_log(self):
|
|
|
|
"""
|
2013-02-23 08:46:01 +00:00
|
|
|
Returns the last logged request, or None.
|
2012-07-30 05:29:36 +00:00
|
|
|
"""
|
|
|
|
l = self.log()
|
|
|
|
if not l:
|
2013-02-23 08:46:01 +00:00
|
|
|
return None
|
2012-07-30 05:29:36 +00:00
|
|
|
return l[-1]
|
|
|
|
|
2012-06-21 03:39:40 +00:00
|
|
|
def log(self):
|
|
|
|
"""
|
|
|
|
Return the log buffer as a list of dictionaries.
|
|
|
|
"""
|
2015-05-30 00:03:13 +00:00
|
|
|
resp = requests.get("%s/api/log" % self.urlbase, verify=False)
|
2012-12-30 23:23:42 +00:00
|
|
|
return resp.json()["log"]
|
2012-06-21 03:39:40 +00:00
|
|
|
|
|
|
|
def clear_log(self):
|
|
|
|
"""
|
|
|
|
Clear the log.
|
|
|
|
"""
|
2015-05-30 00:03:13 +00:00
|
|
|
resp = requests.get("%s/api/clear_log" % self.urlbase, verify=False)
|
2012-06-21 03:39:40 +00:00
|
|
|
return resp.ok
|
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
def shutdown(self):
|
2012-06-21 03:39:40 +00:00
|
|
|
"""
|
|
|
|
Shut the daemon down, return after the thread has exited.
|
|
|
|
"""
|
2012-06-19 01:23:07 +00:00
|
|
|
self.thread.server.shutdown()
|
2012-06-09 08:42:43 +00:00
|
|
|
self.thread.join()
|
2012-06-07 04:35:54 +00:00
|
|
|
|
|
|
|
|
2012-07-30 05:29:36 +00:00
|
|
|
class _PaThread(threading.Thread):
|
|
|
|
def __init__(self, iface, q, ssl, daemonargs):
|
2012-06-07 04:35:54 +00:00
|
|
|
threading.Thread.__init__(self)
|
2014-02-08 21:42:27 +00:00
|
|
|
self.name = "PathodThread"
|
2012-07-30 05:29:36 +00:00
|
|
|
self.iface, self.q, self.ssl = iface, q, ssl
|
2012-07-24 22:34:57 +00:00
|
|
|
self.daemonargs = daemonargs
|
2012-06-07 04:35:54 +00:00
|
|
|
|
|
|
|
def run(self):
|
2012-06-24 03:12:31 +00:00
|
|
|
self.server = pathod.Pathod(
|
2012-07-30 05:29:36 +00:00
|
|
|
(self.iface, 0),
|
2013-01-05 03:48:49 +00:00
|
|
|
ssl = self.ssl,
|
2015-05-30 05:43:01 +00:00
|
|
|
logfp = None,
|
2012-07-24 22:34:57 +00:00
|
|
|
**self.daemonargs
|
2012-06-24 03:12:31 +00:00
|
|
|
)
|
2014-10-24 01:01:34 +00:00
|
|
|
self.name = "PathodThread (%s:%s)" % (
|
|
|
|
self.server.address.host,
|
|
|
|
self.server.address.port
|
|
|
|
)
|
2014-01-28 18:28:20 +00:00
|
|
|
self.q.put(self.server.address.port)
|
2012-06-19 01:23:07 +00:00
|
|
|
self.server.serve_forever()
|