无法连接到 Browsermob-proxy ProxyServerError

Can't connect to Browsermob-proxy ProxyServerError

我刚开始使用 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

如有任何帮助,我们将不胜感激!

我做了一些更改后就可以使用了

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