2012-06-06 23:39:37 +00:00
|
|
|
import json, threading, Queue
|
2012-06-06 23:23:23 +00:00
|
|
|
import requests
|
|
|
|
import pathod
|
|
|
|
|
2012-06-06 23:39:37 +00:00
|
|
|
IFACE = "127.0.0.1"
|
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
class PaThread(threading.Thread):
|
|
|
|
def __init__(self, q, app):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.q = q
|
|
|
|
self.app = app
|
|
|
|
self.port = None
|
|
|
|
|
|
|
|
def run(self):
|
2012-06-06 23:39:37 +00:00
|
|
|
self.server, self.port = pathod.make_server(self.app, 0, IFACE, None)
|
2012-06-06 23:23:23 +00:00
|
|
|
self.q.put(self.port)
|
|
|
|
pathod.run(self.server)
|
|
|
|
|
|
|
|
|
|
|
|
class Daemon:
|
|
|
|
def __init__(self, staticdir=None, anchors=()):
|
|
|
|
self.app = pathod.make_app(staticdir=staticdir, anchors=anchors)
|
|
|
|
self.q = Queue.Queue()
|
|
|
|
self.thread = PaThread(self.q, self.app)
|
|
|
|
self.thread.start()
|
|
|
|
self.port = self.q.get(True, 5)
|
2012-06-06 23:39:37 +00:00
|
|
|
self.urlbase = "http://%s:%s"%(IFACE, self.port)
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2012-06-06 23:39:37 +00:00
|
|
|
def info(self):
|
|
|
|
resp = requests.get("%s/api/info"%self.urlbase)
|
|
|
|
if resp.ok:
|
|
|
|
return json.loads(resp.read())
|
2012-06-06 23:23:23 +00:00
|
|
|
|
|
|
|
def shutdown(self):
|
2012-06-06 23:39:37 +00:00
|
|
|
requests.post("%s/api/shutdown"%self.urlbase)
|