mitmproxy/libpathod/test.py

63 lines
1.8 KiB
Python
Raw Normal View History

import json, threading, Queue
import requests
import pathod, utils
2012-06-09 03:08:51 +00:00
import tutils
IFACE = "127.0.0.1"
class Daemon:
def __init__(self, staticdir=None, anchors=(), ssl=None):
self.q = Queue.Queue()
2012-06-19 04:57:57 +00:00
self.thread = PaThread(self.q, 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.
"""
2012-06-19 01:23:07 +00:00
self.thread.server.shutdown()
self.thread.join()
class PaThread(threading.Thread):
2012-06-19 04:57:57 +00:00
def __init__(self, q, ssl):
threading.Thread.__init__(self)
2012-06-19 04:57:57 +00:00
self.q, self.ssl = q, ssl
self.port = None
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
2012-06-19 22:59:38 +00:00
self.server = pathod.Pathod((IFACE, 0), ssloptions=ssloptions)
2012-06-19 01:23:07 +00:00
#self.server, self.port = pathod.make_server(self.app, 0, IFACE, ssloptions)
self.q.put(self.server.port)
self.server.serve_forever()