我有一个MITMProxy,它在尝试注入JS时偶尔会失败。我正在尝试建立流监视,以检查何时获得2 403状态代码和5 200状态代码来触发警报。这是我的代码:
from bs4 import BeautifulSoup
from mitmproxy import ctx
forbid_count = 0
success_count = 0
with open('inject_javascript.js', 'r') as f:
injected_javascript = f.read()
def response(flow):
global success_count
global forbid_count
if forbid_count == 2 and success_count == 5:
print('############## FAILED ###############')
else:
if flow.response.status_code == 403:
forbid_count = forbid_count + 1
print(forbid_count)
if flow.response.status_code == 200:
success_count = success_count + 1
print(success_count)
if flow.response.headers['Content-Type'] != 'text/html':
return
if not flow.response.status_code == 200:
return
html = BeautifulSoup(flow.response.text, 'lxml')
container = html.head or html.body
if container:
script = html.new_tag('script', type='text/javascript')
script.string = injected_javascript
container.insert(0, script)
flow.response.text = str(html)
ctx.log.info('############### JAVASCRIPT INJECTED ###############')
它显示forbid_count和success_count的计数,但不显示FAILED消息。有更好的方法吗?