36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
|
import utils
|
||
|
|
||
|
def network_listener(tab):
|
||
|
try:
|
||
|
# 监听网络
|
||
|
print("[network.log] 开始监听网络")
|
||
|
tab.listen.start(targets=["/api/"])
|
||
|
for log in tab.listen.steps():
|
||
|
print("=" * 50)
|
||
|
print(f"[network.log] {log.method} {log.url} {'request'} {''}")
|
||
|
print("=" * 50 + '\t' + "request")
|
||
|
print("=" * 50 + '\t' + "request.headers")
|
||
|
for key in log.request.headers.keys():
|
||
|
print(key, log.request.headers[key])
|
||
|
|
||
|
if 'Content-Type' in log.request.headers:
|
||
|
reqType = log.request.headers['Content-Type']
|
||
|
if reqType == 'application/json':
|
||
|
print("=" * 50 + '\t' + "request.body")
|
||
|
print(utils.format_json(log.request.body))
|
||
|
|
||
|
print(vars(log.request))
|
||
|
|
||
|
if 'Content-Type' in log.response.headers:
|
||
|
respType = log.response.headers['Content-Type']
|
||
|
if respType == 'application/json':
|
||
|
print("=" * 50 + '\t' + "response.body")
|
||
|
print(utils.format_json(log.response.body))
|
||
|
|
||
|
print(vars(log.response))
|
||
|
except Exception as e:
|
||
|
print("[network.log] 监听网络出错")
|
||
|
print(e)
|
||
|
finally:
|
||
|
print("[network.log] 停止监听网络")
|
||
|
tab.listen.stop()
|