无法连接到Browsermob-proxy ProxyServerError

时间:2019-11-27 00:15:02

标签: python selenium-webdriver browsermob-proxy browsermob

我是第一次使用browsermob-proxy实用工具。这是我的代码,可以在本地实例上很好地运行,但是当我在ec2实例上尝试(使用无头浏览器)时出现错误:

 raise ProxyServerError("Can't connect to Browsermob-Proxy")
browsermobproxy.exceptions.ProxyServerError: Can't connect to Browsermob-Proxy

我的本​​地实例设置和ec2实例设置之间的唯一区别是,在ec2上,它运行在无头浏览器上。

代码段:

def start_proxy_server():
    for proc in psutil.process_iter():
        # check whether the process name matches
        if proc.name() == "browsermob-proxy":
            proc.kill()
    dict = {'port': 7190}
    server = Server(path="path_to_browswermob/browsermob-proxy-2.1.4/bin/browsermob-proxy", options=dict)
    server.start()
    time.sleep(1)
    proxy = server.create_proxy()
    time.sleep(1)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy)) #Configure chrome options
    driver = webdriver.Chrome('path_to_chromedriver/chromedriver',options=chrome_options)
    options = Options()
    options.headless = True
    driver.get("www.google.com")
    return driver, proxy

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我进行了一些更改

def start_proxy_server():
for proc in psutil.process_iter():
    # check whether the process name matches
    if proc.name() == "browsermob-proxy":
        proc.kill()

dict = {'port': 8080}
server = Server(path="path_to_browswermob/browsermob-proxy-2.1.4/bin/browsermob-proxy", options=dict)
server.start()
time.sleep(1)
proxy = server.create_proxy()
time.sleep(1)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy)) #Configure chrome options