如何使用browsermob-proxy修复“地址已在使用中”错误? [英] How to fix 'Address already in use' error with browsermob-proxy?

查看:1014
本文介绍了如何使用browsermob-proxy修复“地址已在使用中”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在尝试使用 browsermob-proxy 此处)以尝试在python硒测试期间监视网络流量(请参见 browsermobproxypy )。我正在使用以下脚本对其进行测试:

I am still trying to use browsermob-proxy (here) to try to monitor the network traffic during a python selenium test (see browsermobproxypy). I am using the following script to test it:

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文件(在文档此处并没有很好地描述);您必须从此处)下载jar文件。我正在按以下方式启动代理:

I somehow managed to install a 'browsermob.jar' file (which is not really nicely described in the documentation here; you have to download the jar files from here). I am starting the proxy as follows:

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

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

However, in the logs I see errors like follows:

[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文档实际上不存在...

My best guess is that the port is already in use, or something else is not working correctly. Maybe I need to explicitly specify the port in the script? But how to do that? A python documentation is virtually not-existing ...


  • 查看源代码(!),我弄清楚了如何设置端口。我使用以下行在python脚本中定义服务器和端口:

  • Looking at the source code(!) I figured out how to set the port. I am using the following line to define the server and the port in the python script:

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

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

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

But no matter what I set as the port I always get the following error:

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...

因此,可能还有其他工作不正常...

So there must be something else working incorrectly...

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

Maybe I will just code such a proxy on my own. Seems to be the faster way...

推荐答案

我们可以使用所需端口启动BMP服务器,如下所示。服务器接受字典对象作为服务器参数。

We can start the BMP server with required port as given below. server accepts dictionary object for server arguments.

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()

这篇关于如何使用browsermob-proxy修复“地址已在使用中”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆