如何修复'已使用的地址' browsermob-proxy错误?

时间:2018-01-18 06:55:16

标签: python selenium proxy browsermob-proxy

我仍在尝试使用browsermob-proxyhere)尝试在python selenium测试期间监控网络流量(请参阅browsermobproxypy)。我使用以下脚本来测试它:

from browsermobproxy import Server
server = Server("/home/adietz/Projects/Invest/browsermob-proxy/browsermob-proxy-2.1.4/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()

from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)


proxy.new_har("google")
driver.get("http://www.google.co.in")
proxy.har

server.stop()
driver.quit()

我设法安装了一个' browsermob.jar'文件(文档here中没有很好地描述);您必须从here下载jar文件。我按如下方式启动代理:

java -jar ./browsermob-proxy-2.1.4/lib/browsermob-dist-2.1.4.jar --port 9090
Running BrowserMob Proxy using LittleProxy implementation. To revert to the legacy implementation, run the proxy with the command-line option '--use-littleproxy false'.
[INFO  2018-01-18T07:47:20,761 net.lightbody.bmp.proxy.Main] (main) Starting BrowserMob Proxy version 2.1.4 
[INFO  2018-01-18T07:47:20,782 org.eclipse.jetty.util.log] (main) jetty-7.x.y-SNAPSHOT 
[INFO  2018-01-18T07:47:20,802 org.eclipse.jetty.util.log] (main) started o.e.j.s.ServletContextHandler{/,null} 
[INFO  2018-01-18T07:47:20,885 org.eclipse.jetty.util.log] (main) Started SelectChannelConnector@0.0.0.0:9090 

然后我将测试脚本作为

运行
python tester1.py

但是,在日志中我看到如下错误:

[WARN  2018-01-18T07:28:30,265 org.eclipse.jetty.util.log] (main) FAILED SelectChannelConnector@0.0.0.0:8080: java.net.BindException: Address already in use 
[WARN  2018-01-18T07:28:30,266 org.eclipse.jetty.util.log] (main) FAILED org.eclipse.jetty.server.Server@4b41e4dd: java.net.BindException: Address already in use 
[ERROR 2018-01-18T07:28:30,266 net.lightbody.bmp.proxy.Main] (main) Failed to start Jetty server. Aborting. java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_151]
    at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_151]
    at sun.nio.ch.Net.bind(Net.java:425) ~[?:1.8.0_151]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[?:1.8.0_151]
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[?:1.8.0_151]
    at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:162) ~[browsermob-dist-2.1.4.jar:?]
    at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:356) ~[browsermob-dist-2.1.4.jar:?]
    at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:240) ~[browsermob-dist-2.1.4.jar:?]
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55) ~[browsermob-dist-2.1.4.jar:?]
    at org.eclipse.jetty.server.Server.doStart(Server.java:269) ~[browsermob-dist-2.1.4.jar:?]
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55) ~[browsermob-dist-2.1.4.jar:?]
    at net.lightbody.bmp.proxy.Main.main(Main.java:64) [browsermob-dist-2.1.4.jar:?]

我最好的猜测是端口已经在使用,或者其他东西无法正常工作。也许我需要在脚本中明确指定端口?但是怎么做呢? python文档几乎不存在......

附录

  • 查看源代码(!)我想出了如何设置端口。我使用以下行来定义python脚本中的服务器和端口:

    server = Server(" /home/adietz/Projects/Invest/browsermob-proxy/browsermob-proxy-2.1.4/bin/browsermob-proxy" ;, {' port' :9090})

但无论我设置什么端口,我总是会收到以下错误:

Traceback (most recent call last):
  File "tester1.py", line 5, in <module>
    proxy = server.create_proxy()
  File "/home/adietz/Projects/Invest/browsermob-proxy/venv_browsermob/local/lib/python2.7/site-packages/browsermobproxy/server.py", line 40, in create_proxy
    client = Client(self.url[7:], params)
  File "/home/adietz/Projects/Invest/browsermob-proxy/venv_browsermob/local/lib/python2.7/site-packages/browsermobproxy/client.py", line 37, in __init__
    "Another server running on this port?\n%s..." % content[:512])
Exception: Could not read Browsermob-Proxy json
Another server running on this port?
java.lang.RuntimeException: java.net.BindException: Address already in use
    at org.littleshoot.proxy.impl.DefaultHttpProxyServer.doStart(DefaultHttpProxyServer.java:545)
    at org.littleshoot.proxy.impl.DefaultHttpProxyServer.start(DefaultHttpProxyServer.java:489)
    at org.littleshoot.proxy.impl.DefaultHttpProxyServer.access$700(DefaultHttpProxyServer.java:69)
    at org.littleshoot.proxy.impl.DefaultHttpProxyServer$DefaultHttpProxyServerBootstrap.start(DefaultHttpProxyServer.java:858)
    at net.lightbody.bmp.Brows...

所以必须有其他工作不正确......

也许我会自己编写这样的代理。似乎是更快的方式...

2 个答案:

答案 0 :(得分:1)

我们可以使用下面给出的所需端口启动BMP服务器。 server接受服务器参数的字典对象。

from browsermobproxy import Server
dict={'port':8090}
server = Server(path="C:\\Projects\\BrowserMobProxy\\bin\\browsermob-proxy",options=dict)
server.start()
proxy = server.create_proxy()

from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)


proxy.new_har("google")
driver.get("http://www.google.co.uk")
proxy.har # returns a HAR JSON blob

server.stop()
driver.quit()

答案 1 :(得分:0)

以此初始化服务器-

proxyserver =  Server(
       path="/root/tests/bsp_usecase_tests/bin/browsermob-proxy",
       options={'existing_proxy_port_to_use': 8090})

Browsermob-Proxy中的内部代码正在寻找名为'options'的{​​{1}}字典中的密钥,否则它将使用默认的即空字典。