refactor: 重构监听功能并分离到独立模块
将控制台和网络监听功能从 `main.py` 中分离到 `listener` 模块,以提高代码的可维护性和可读性。同时,新增 `utils` 模块用于格式化 JSON 数据,并更新 `requirements.txt` 以包含必要的依赖。
This commit is contained in:
parent
f92937fba6
commit
deb51d8579
0
listener/__init__.py
Normal file
0
listener/__init__.py
Normal file
15
listener/console.py
Normal file
15
listener/console.py
Normal file
@ -0,0 +1,15 @@
|
||||
from reloading import reloading
|
||||
|
||||
|
||||
def console_listener(tab):
|
||||
try:
|
||||
# 监听控制台
|
||||
print("[console.log] 开始监听控制台")
|
||||
tab.console.start()
|
||||
steps = tab.console.steps()
|
||||
for log in steps:
|
||||
# print("[console.log]", log)
|
||||
print(f"[console.log] {log.text}")
|
||||
finally:
|
||||
print("[console.log] 停止监听控制台")
|
||||
tab.console.stop()
|
36
listener/network.py
Normal file
36
listener/network.py
Normal file
@ -0,0 +1,36 @@
|
||||
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()
|
50
main.py
50
main.py
@ -1,54 +1,26 @@
|
||||
from DrissionPage import Chromium, ChromiumOptions, SessionPage
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import atexit
|
||||
import json
|
||||
|
||||
import listener.console
|
||||
import listener.network
|
||||
|
||||
|
||||
options = ChromiumOptions()
|
||||
# 无头模式
|
||||
# options.headless()
|
||||
|
||||
import os
|
||||
os.environ.setdefault('PYTHONIOENCODING', 'utf-8')
|
||||
print(os.environ)
|
||||
|
||||
browser = Chromium(options)
|
||||
tab = browser.latest_tab
|
||||
|
||||
|
||||
def console_listener(tab):
|
||||
# 监听控制台
|
||||
tab.console.start()
|
||||
steps = tab.console.steps()
|
||||
for log in steps:
|
||||
# print("[console.log]", log)
|
||||
print(f"[console.log] {log.text}")
|
||||
|
||||
def network_listener(tab):
|
||||
# 监听网络
|
||||
tab.listen.start(targets=["/api/"])
|
||||
for log in tab.listen.steps():
|
||||
try:
|
||||
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])
|
||||
|
||||
reqType = log.request.headers['Content-Type']
|
||||
if reqType == 'application/json':
|
||||
print("=" * 50 + '\t' + "request.body")
|
||||
print(json.dumps(log.request.body, indent=4))
|
||||
print(vars(log.request))
|
||||
|
||||
respType = log.response.headers['Content-Type']
|
||||
if respType == 'application/json':
|
||||
print("=" * 50 + '\t' + "response.body")
|
||||
print(json.dumps(log.response.body, indent=4))
|
||||
|
||||
print(vars(log.response))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
executor = ThreadPoolExecutor(max_workers=2)
|
||||
executor.submit(console_listener, tab)
|
||||
executor.submit(network_listener, tab)
|
||||
executor.submit(listener.console.console_listener, tab)
|
||||
executor.submit(listener.network.network_listener, tab)
|
||||
|
||||
|
||||
tab.get(url='http://127.0.0.1:24613')
|
||||
tab.change_mode()
|
||||
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
DrissionPage==4.1.0.17
|
4
utils/__init__.py
Normal file
4
utils/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
import json
|
||||
def format_json(data):
|
||||
"""格式化JSON数据"""
|
||||
return json.dumps(data, indent=4, ensure_ascii=False)
|
Loading…
Reference in New Issue
Block a user