AutomatedTester / browsermob-proxy-py

A python wrapper for Browsermob Proxy
http://oss.theautomatedtester.co.uk/browsermob-proxy-py
236 stars 104 forks source link

Exception: Can't connect to Browsermob-Proxy #30

Closed chiedo closed 9 years ago

chiedo commented 9 years ago

Hey, I'm getting the following error on server.start()

Exception: Can't connect to Browsermob-Proxy

I have the following:

from browsermobproxy import Server
server = Server("/Users/username/Desktop/browsermob-proxy/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()

I am on a Mac with 10.10.1. I downloaded the browsermob-proxy files from http://bmp.lightbody.net/

I am able to successfully start the proxy server manually with /Users/username/Desktop/browsermob-proxy/bin/browsermob-proxy -port 9090

My apologies if I'm missing something obvious

chiedo commented 9 years ago

Obvious. I was not supposed to set the "-port" flag.

AutomatedTester commented 9 years ago

Glad you sorted things out 😀

— Sent from Mailbox

On Fri, Feb 20, 2015 at 6:14 PM, Chiedo John notifications@github.com wrote:

Obvious. I was not supposed to set the "-port" flag.

Reply to this email directly or view it on GitHub: https://github.com/AutomatedTester/browsermob-proxy-py/issues/30#issuecomment-75290710

chiedo commented 9 years ago

Hahaha, thanks man. It was definitely a noob move on my part... but maybe change the readme to something like the following? Obviously you can ignore my suggestion though!

browsermob-proxy-py
===================

Python client for the BrowserMob Proxy 2.0 REST API.

How to use with selenium-webdriver
----------------------------------

Manually:

``` python 
from browsermobproxy import Server
server = Server("path/to/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.uk")
proxy.har # returns a HAR JSON blob

server.stop()
driver.quit()

for Chrome use

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
browser = webdriver.Chrome(chrome_options = chrome_options)

Before Running Tests

Start the browsermob-proxy with the following in your terminal and wait for the proxy to start:

path/to/browsermob-proxy

Running Tests

To run the tests in a CI environment, disable the ones that require human judgement by using

$ py.test -m "not human" test

If you are going to watch the test, the 'human' ones should display an english muffin instead of the american flag on the 'pick your version' page. Or at least it does from Canada.

See also

Note on Patches/Pull Requests

Copyright

Copyright 2011 David Burns

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

AutomatedTester commented 9 years ago

Feel free to help me with docs and the readme by submitting a pull request 😉

— Sent from Mailbox

On Sat, Feb 21, 2015 at 3:04 PM, Chiedo John notifications@github.com wrote:

Hahaha, thanks man. It was definitely a noob move on my part... but maybe change the readme to something like the following? Obviously you can ignore my suggestion though!

browsermob-proxy-py
===================
Python client for the BrowserMob Proxy 2.0 REST API.
How to use with selenium-webdriver
----------------------------------
Manually:
``` python 
from browsermobproxy import Server
server = Server("path/to/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.uk")
proxy.har # returns a HAR JSON blob
server.stop()
driver.quit()

for Chrome use

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
browser = webdriver.Chrome(chrome_options = chrome_options)

Before Running Tests

Start the browsermob-proxy with the following in your terminal and wait for the proxy to start:

path/to/browsermob-proxy

Running Tests

To run the tests in a CI environment, disable the ones that require human judgement by using

$ py.test -m "not human" test

If you are going to watch the test, the 'human' ones should display an english muffin instead of the american flag on the 'pick your version' page. Or at least it does from Canada.

See also

  • http://proxy.browsermob.com/
  • https://github.com/webmetrics/browsermob-proxy Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Send me a pull request. Bonus points for topic branches. Copyright

    Copyright 2011 David Burns Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

---
Reply to this email directly or view it on GitHub:
https://github.com/AutomatedTester/browsermob-proxy-py/issues/30#issuecomment-75375393
chiedo commented 9 years ago

Fair enough! :)

mhmughees commented 9 years ago

HI I am receiving the very same error while trying to start the server.

import sys from browsermobproxy import Server from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities server = Server('C:/browsermob-proxy/bin/browsermob-proxy') server.start()

Kindly please let me know what I am doing wrong

mhmughees commented 9 years ago

oh it was a problem of administrative rights on py file

aerickson commented 9 years ago

@chiedojohn, I feel like you got misled a little. You can definitely run browsermob on a different port and still use this library... you just need to pass in an optional secondary argument - the options dict with something like:

server = Server("path/to/browsermob-proxy", options={'port': 9999}.)

https://github.com/AutomatedTester/browsermob-proxy-py/search?utf8=%E2%9C%93&q=8080

chiedo commented 9 years ago

Thanks @aerickson, I actually came to that same conclusion a few weeks ago!

pronojitsaha commented 9 years ago

Hi,

I'm getting the same error on server.start()

Exception: Can't connect to Browsermob-Proxy

I also have the following:

from browsermobproxy import Server
server = Server("/Users/username/Desktop/browsermob-proxy/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy() 

Further on terminal, if I do /Users/username/Desktop/browsermob-proxy/bin/browsermob-proxy -port 9090 I get the following error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: net/lightbody/bmp/proxy/Main : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

It seems to be some issue with the version number, but not sure what exactly it is. Thanks.

kinkwong commented 9 years ago
from browsermobproxy import Server 
server = Server("browsermob-proxy-2.1.0-beta-3/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()

I run the four lines of codes and get the following error in the last line.

>>> proxy = server.create_proxy()
...
File "C:\Python27\lib\site-packages\requests\adapters.py", line 415, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))

I could run the four lines successfully for the first time, but after a reboot I always get this error message. The same happen in my second machine. Did I forget to release the resource or something in my first run?

javariafarhan commented 8 years ago

I am getting the same error when trying to run the following .py script:

`import os from browsermobproxy import Server

server = Server("/Users/javaria/Desktop/browsermob-proxy-2.0-beta-6/bin/browsermob-proxy", options={'port': 9090}) 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()`

I am getting this error in console File "test1.py", line 6, in <module> server.start() File "/Users/javaria/Documents/projects/proxy-testing/browsermob-proxy-py/browsermobproxy/server.py", line 122, in start raise Exception(message) Exception: The Browsermob-Proxy server process failed to start. Check <open file '/server.log', mode 'w' at 0x10d55e4b0>for a helpful error message.

I have set my $JAVA_HOME in ~/.bash_profile, the proxy works just fine manually but it just won't run from the python script. It's not an issue with file permission as well since I'm running the script with sudo. I am new to automation and would really appreciate your help here.

sparkline-francisv commented 7 years ago

I have the same issue as on:

Here's the sample code:

from browsermobproxy import Server
server = Server("C:\\tools\\browsermob-proxy-2.1.1\\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.uk")
proxy.har # returns a HAR JSON blob
server.stop()
driver.quit()

and here's the output

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\browsermobproxy\client.py", line 34, in __init__
    jcontent = json.loads(content)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "temp.py", line 4, in <module>
    proxy = server.create_proxy()
  File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\browsermobproxy\server.py", line 40, in create_proxy
    client = Client(self.url[7:], params)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\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: bind
        at org.littleshoot.proxy.impl.DefaultHttpProxyServer.doStart(DefaultHttpProxyServer.java:519)
        at org.littleshoot.proxy.impl.DefaultHttpProxyServer.start(DefaultHttpProxyServer.java:463)
        at org.littleshoot.proxy.impl.DefaultHttpProxyServer.access$700(DefaultHttpProxyServer.java:70)
        at org.littleshoot.proxy.impl.DefaultHttpProxyServer$DefaultHttpProxyServerBootstrap.start(DefaultHttpProxyServer.java:799)
        at net.lightbod...
geraldini commented 7 years ago

I'm also hitting this issue with version 2.1.4.

When I do this on the interpreter, I get the below output. Notice that I only hit the error the first time...

>>> server.start()
>>> proxy = server.create_proxy()
Traceback (most recent call last):
  File "/Users/npassosg/.virtualenvs/wan-staging/lib/python3.6/site-packages/browsermobproxy/client.py", line 34, in __init__
    jcontent = json.loads(content)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/npassosg/.virtualenvs/wan-staging/lib/python3.6/site-packages/browsermobproxy/server.py", line 40, in create_proxy
    client = Client(self.url[7:], params)
  File "/Users/npassosg/.virtualenvs/wan-staging/lib/python3.6/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...
>>> proxy = server.create_proxy()
>>>

This is what I see in the proxy process:

Running BrowserMob Proxy using LittleProxy implementation. To revert to the legacy implementation, run the proxy with the command-line option '--use-littleproxy false'.
[INFO  2017-07-05T18:30:37,593 net.lightbody.bmp.proxy.Main] (main) Starting BrowserMob Proxy version 2.1.4
[INFO  2017-07-05T18:30:37,645 org.eclipse.jetty.util.log] (main) jetty-7.x.y-SNAPSHOT
[INFO  2017-07-05T18:30:37,694 org.eclipse.jetty.util.log] (main) started o.e.j.s.ServletContextHandler{/,null}
[INFO  2017-07-05T18:30:37,878 org.eclipse.jetty.util.log] (main) Started SelectChannelConnector@0.0.0.0:8080
[INFO  2017-07-05T18:31:00,667 org.littleshoot.proxy.impl.DefaultHttpProxyServer] (qtp1449605932-16) Starting proxy at address: 0.0.0.0/0.0.0.0:8081
[INFO  2017-07-05T18:31:00,688 org.littleshoot.proxy.impl.DefaultHttpProxyServer] (qtp1449605932-16) Proxy listening with TCP transport
[WARN  2017-07-05T18:31:00,760 net.lightbody.bmp.BrowserMobProxyServer] (qtp1449605932-16) Attempted to stop proxy server, but proxy was never successfully started.
[INFO  2017-07-05T18:31:14,204 org.littleshoot.proxy.impl.DefaultHttpProxyServer] (qtp1449605932-21) Starting proxy at address: 0.0.0.0/0.0.0.0:8082
[INFO  2017-07-05T18:31:14,205 org.littleshoot.proxy.impl.DefaultHttpProxyServer] (qtp1449605932-21) Proxy listening with TCP transport
[INFO  2017-07-05T18:31:14,207 org.littleshoot.proxy.impl.DefaultHttpProxyServer] (qtp1449605932-21) Proxy started at address: /0:0:0:0:0:0:0:0:8082
sparkline-francisv commented 7 years ago

It appears that even if you start browsermob-proxy (2.1.1 and 2.1.4) on a different port (e.g. 7070), it will still spew out a proxy starting at 8081:

λ bin\browsermob-proxy.bat -port 7070
Running BrowserMob Proxy using LittleProxy implementation. To revert to the legacy implementation, run the proxy with the command-line option '--use-littleproxy false'.
[INFO  2017-07-10T10:27:59,972 net.lightbody.bmp.proxy.Main] (main) Starting BrowserMob Proxy version 2.1.1
[INFO  2017-07-10T10:28:00,027 org.eclipse.jetty.util.log] (main) jetty-7.x.y-SNAPSHOT
[INFO  2017-07-10T10:28:00,403 org.eclipse.jetty.util.log] (main) started o.e.j.s.ServletContextHandler{/,null}
[INFO  2017-07-10T10:28:00,760 org.eclipse.jetty.util.log] (main) Started SelectChannelConnector@0.0.0.0:7070

I have another process running on port 8081 before I executed the curl request.

λ netstat -ano | find "8081"
  TCP    0.0.0.0:8081           0.0.0.0:0              LISTENING       1128
  TCP    [::]:8081              [::]:0                 LISTENING       1128

λ curl -X POST http://localhost:7070/proxy
java.lang.RuntimeException: java.net.BindException: Address already in use: bind
        at org.littleshoot.proxy.impl.DefaultHttpProxyServer.doStart(DefaultHttpProxyServer.java:519)
        at org.littleshoot.proxy.impl.DefaultHttpProxyServer.start(DefaultHttpProxyServer.java:463)
        at org.littleshoot.proxy.impl.DefaultHttpProxyServer.access$700(DefaultHttpProxyServer.java:70)
        at org.littleshoot.proxy.impl.DefaultHttpProxyServer$DefaultHttpProxyServerBootstrap.start(DefaultHttpProxyServer.java:799)
...

On the terminal where I launched browsermob-proxy, I get this message:

[INFO  2017-07-10T10:40:53,245 org.littleshoot.proxy.impl.DefaultHttpProxyServer] (qtp664186542-15) Starting proxy at address: 0.0.0.0/0.0.0.0:8081
[INFO  2017-07-10T10:40:53,278 org.littleshoot.proxy.impl.DefaultHttpProxyServer] (qtp664186542-15) Proxy listening with TCP transport
[WARN  2017-07-10T10:40:53,611 net.lightbody.bmp.BrowserMobProxyServer] (qtp664186542-15) Attempted to stop proxy server, but proxy was never successfully started.
andreabisello commented 5 years ago

my problem was with jre not installed.

sudo apt-get install default-jre solved my problem.

lnataraj commented 5 months ago

I'm having a problem with creating proxy , the process is hanging and proxy = server.create_proxy()

Error 500 Exception [NoClassDefFoundError - "Could not initialize class io.netty.util.concurrent.GlobalEventExecutor"] thrown by event method [public com.google.sitebricks.headless.Reply net.lightbody.bmp.proxy.bricks.ProxyResource.newProxy(com.google.sitebricks.headless.Request)] at org.littleshoot.proxy.impl.DefaultHttpProxyServer.<init>(DefaultHttpProxyServer.java:138) (See below for entire trace.) </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>