Search code examples
pythonselenium-webdriverbrowsermob-proxybrowsermob

Can't connect to Browsermob-proxy ProxyServerError


I'm new to using browsermob-proxy utility tool. Here's my code that runs perfectly fine on my local instance but when I try it on my ec2 instance(using headless browser) gives me an error:

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

The only difference between my local and ec2 instance setup is that on ec2 it's running on the headless browser.

Code Snippet:

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

Any help would be appreciated!


Solution

  • I got it working with some changes

    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