简体   繁体   English

Browsermob代理-如何使用response_interceptor修改响应?

[英]Browsermob proxy - how can I modify response using response_interceptor?

I am using a python wrapper for browsermob proxy , and I need to modify the server response. 我正在使用python 包装器作为browsermob代理 ,并且需要修改服务器响应。 But I can't figure out how I can do it. 但是我不知道该怎么做。 Can anybody give me a working code sample? 有人可以给我一个工作代码示例吗?

The documentation says I can send a javascript code for it, but it does not work for me: 该文档说我可以为此发送一个javascript代码,但是它对我不起作用:

server = Server(path=SCRIPT_DIR+"/browsermob-proxy/bin/browsermob-proxy", options={"port": 8888})
server.start()
proxy = server.create_proxy()

proxy.response_interceptor('contents.setTextContents("<html><body>Response successfully intercepted</body></html>")')

fp = webdriver.FirefoxProfile()
browser = webdriver.Firefox(firefox_profile=fp, proxy=proxy)
browser.get("http://example.com")

Thanks! 谢谢!

In browsermobproxy version 0.7.1, response_interceptor is bugged. 在浏览器mobproxy 0.7.1版中,response_interceptor发生了错误。 You can go to github and get the latest code or you can use this work-around instead of response_interceptor: 您可以转到github并获取最新代码,也可以使用以下变通方法代替response_interceptor:

import requests

url = proxy.host + '/proxy/' + str(proxy.port) + '/filter/response'
headers={'Content-Type':'text/plain'}

filter = "contents.setTextContents('<html><body>Response successfully intercepted</body></html>');"

r = requests.post(url=url, data=filter, headers=headers)

Works like a charm - just don't forget to set proxy on your Firefox profile ;) fp.set_proxy(proxy.selenium_proxy()) 就像魅力一样工作-只是不要忘记在Firefox配置文件上设置代理;)fp.set_proxy(proxy.selenium_proxy())

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from browsermobproxy import Server
from pprint import pprint
from time import sleep
import requests


server = Server($PATH_GOES_HERE$)
server.start()


proxy = server.create_proxy()

proxy.new_har('test111')

url = proxy.host + '/proxy/' + str(proxy.port) + '/filter/response'
headers={'Content-Type':'text/plain'}

filter = "response.setTextContents('<html><body>Response successfully intercepted</body></html>');"

r = requests.post(url=url, data=filter, headers=headers)





fp = webdriver.FirefoxProfile()
fp.set_proxy(proxy.selenium_proxy()) # <---- you forgot this one here ;)
browser = webdriver.Firefox(firefox_profile=fp, proxy=proxy)
sleep(3)
browser.get("example.com")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM