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
|
import sys,requests,json,datetime from gor.middleware import TornadoGor
def log(msg): """ Logging to STDERR as STDOUT and STDIN used for data transfer @type msg: str or byte string @param msg: Message to log to STDERR """ try: msg = str(msg) + '\n' except: pass sys.stderr.write(msg) sys.stderr.flush()
def sendResponse(fromType,id,postData): headers = { 'Content-Type': 'application/json' } data = { 'fromType': fromType, 'id': id, 'postData': postData } res = requests.post('http://127.0.0.1:5000/getResponse',data=json.dumps(data),headers=headers) response = res.json() logTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") log("[{}]: fromType={},id={},{}".format(logTime,fromType,id,response['msg']))
def on_request(proxy, msg, **kwargs): sendResponse(1, msg.id, msg.http) proxy.on('response', on_response, idx=msg.id, req=msg)
def on_response(proxy, msg, **kwargs): sendResponse(2,msg.id,msg.http.split('\r\n\r\n')[1]) proxy.on('replay', on_replay, idx=kwargs['req'].id, req=kwargs['req'], resp=msg)
def on_replay(proxy, msg, **kwargs): sendResponse(3, msg.id, msg.http.split('\r\n\r\n')[1])
if __name__ == '__main__': proxy = TornadoGor() proxy.on('request', on_request) proxy.run()
|