Created
November 28, 2019 05:32
-
-
Save rkdgusrnrlrl/5f3a960af0c135291a6c06d678f13ddc to your computer and use it in GitHub Desktop.
remote debugger network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import websocket | |
import json | |
import requests | |
import signal | |
import sys | |
from os import path | |
import logging | |
logging.basicConfig(filename='network_monitor.log',level=logging.DEBUG) | |
from time import sleep | |
try: | |
import thread | |
except ImportError: | |
import _thread as thread | |
import time | |
network_enable_method = { | |
'id': 0, | |
'method': 'Network.enable' | |
} | |
method_id = 4 | |
requset_dict = dict() | |
response_dict = dict() | |
request_id_dict = dict() | |
response_body_dict = dict() | |
network_list = list() | |
log_dir = '' | |
def find_request(id): | |
for rr in request_list: | |
if rr['id'] == id: | |
return rr | |
return None | |
def on_message(ws, message): | |
logging.info('on message') | |
ee = json.loads(message) | |
if 'method' in ee and ee['method'] == 'Network.responseReceived': | |
rr = dict() | |
dd = ee['params']['response'] | |
rr['url'] = dd['url'] | |
rr['status'] = dd['status'] | |
rr['headers'] = dd['headers'] | |
rr['monotonic_time'] = ee['params']['timestamp'] | |
id = ee['params']['requestId'] | |
response_dict[id] = rr | |
get_response_body(ws, id) | |
elif 'method' in ee and ee['method'] == 'Network.requestWillBeSent': | |
dd = ee['params']['request'] | |
rr = dict() | |
id = ee['params']['requestId'] | |
rr['id'] = id | |
rr['method'] = dd['method'] | |
rr['url'] = dd['url'] | |
rr['headers'] = dd['headers'] | |
if 'postData' in dd: | |
rr['post_data'] = dd['postData'] | |
rr['monotonic_time'] = ee['params']['timestamp'] | |
rr['timestamp'] = ee['params']['wallTime'] | |
requset_dict[id] = rr | |
elif 'result' in ee and 'body' in ee['result']: | |
m_id = ee['id'] | |
request_id = request_id_dict[m_id] | |
body = ee['result']['body'] | |
res = response_dict[request_id] | |
if 'headers' in res and '' in res['headers'] and 'image' not in res['headers']['content-type']: | |
res['body'] = body | |
req = requset_dict[request_id] | |
res['timestamp'] = req['timestamp'] - req['monotonic_time'] + res['monotonic_time'] | |
dd = {'id' : request_id, 'request' : req, 'response' : res} | |
network_list.append(dd) | |
def get_response_body(ws, request_id): | |
global method_id | |
method_id += 1 | |
get_response_body_method = { | |
'id' : method_id, | |
'method': 'Network.getResponseBody', | |
'params': { | |
'requestId' : request_id | |
} | |
} | |
ws.send(json.dumps(get_response_body_method)) | |
request_id_dict[method_id] = request_id | |
def on_error(ws, error): | |
print(error) | |
def on_close(ws): | |
on_end_script(None, None) | |
def on_open(ws): | |
def run(*args): | |
ws.send(json.dumps(network_enable_method)) | |
time.sleep(1) | |
thread.start_new_thread(run, ()) | |
def on_end_script(signal, frame): | |
logging.info('end script') | |
sleep(5) | |
dd = {'network_list' : network_list} | |
ww = json.dumps(dd, sort_keys=True, indent=2) | |
pp = path.join(log_dir, 'network.json') | |
open(pp, "w+").write(ww) | |
exit(0) | |
def _print_how_to(): | |
print('please input path') | |
print('(sample)') | |
print('\t./run_network_monitor.py /network/log/directory') | |
if __name__ == "__main__": | |
logging.info('start script') | |
if len(sys.argv) != 2 \ | |
or not path.exists(sys.argv[1]) \ | |
or not path.isdir(sys.argv[1]): | |
_print_how_to() | |
exit(1) | |
log_dir = sys.argv[1] | |
signal.signal(signal.SIGINT, on_end_script) | |
r = requests.get('http://localhost:9222/json') | |
dd = r.json() | |
websocket_url = dd[0]['webSocketDebuggerUrl'] | |
websocket.enableTrace(False) | |
ws = websocket.WebSocketApp(websocket_url, | |
on_message = on_message, | |
on_error = on_error, | |
on_close = on_close) | |
ws.on_open = on_open | |
ws.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment