I am using BrowserMobProxy to examine the traffic generated by my selenium webdriver. After restarting my PC I receive
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused
message. It looks like it was not stopped correctly, by can it effect my system even after restart? Any advice?
Here is my code:
from browsermobproxy import Server
from selenium import webdriver
class ProxyManager:
browsermobproxy_path = "D:/browsermob-proxy-2.1.4/bin/browsermob-proxy.bat"
def __init__(self):
self.server = Server(ProxyManager.browsermobproxy_path)
self.client = None #will be initialized manually for better control
def start_server(self):
self.server.start()
return self.server
def start_client(self):
#disable certificate check
self.client = self.server.create_proxy(params={"trustALLServers":"true"})
return self.client
if "__main__" == __name__:
proxy = ProxyManager()
server = proxy.start_server()
client = proxy.start_client()
and here is my bmp.log:
[INFO 2020-09-04T01:02:13,322 net.lightbody.bmp.proxy.Main] (main) Starting BrowserMob Proxy version 2.1.4
[INFO 2020-09-04T01:02:13,434 org.eclipse.jetty.util.log] (main) jetty-7.x.y-SNAPSHOT
[INFO 2020-09-04T01:02:13,710 org.eclipse.jetty.util.log] (main) started o.e.j.s.ServletContextHandler{/,null}
[WARN 2020-09-04T01:02:14,170 org.eclipse.jetty.util.log] (main) FAILED [email protected]:8080: java.net.BindException: Address already in use: bind
[WARN 2020-09-04T01:02:14,172 org.eclipse.jetty.util.log] (main) FAILED org.eclipse.jetty.server.Server@56f0cc85: java.net.BindException: Address already in use: bind
[ERROR 2020-09-04T01:02:14,218 net.lightbody.bmp.proxy.Main] (main) Failed to start Jetty server. Aborting. java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_261]
at sun.nio.ch.Net.bind(Unknown Source) ~[?:1.8.0_261]
at sun.nio.ch.Net.bind(Unknown Source) ~[?:1.8.0_261]
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) ~[?:1.8.0_261]
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) ~[?:1.8.0_261]
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:?]
my full error message:
Traceback (most recent call last):
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
chunked=chunked,
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\Kosh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1252, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\Kosh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1298, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\Kosh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1247, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Kosh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "C:\Users\Kosh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 966, in send
self.connect()
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\connection.py", line 187, in connect
conn = self._new_conn()
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000273DE63C5C8>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\connectionpool.py", line 727, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=8866): Max retries exceeded with url: http://localhost:8080/proxy?trustALLServers=true (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000273DE63C5C8>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 23, in start_client
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\browsermobproxy\server.py", line 40, in create_proxy
client = Client(self.url[7:], params)
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\browsermobproxy\client.py", line 31, in __init__
resp = requests.post('%s/proxy' % self.host + urlparams)
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\requests\api.py", line 119, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Kosh\PycharmProjects\CSSSR_Selenium\venv\lib\site-packages\requests\adapters.py", line 510, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='127.0.0.1', port=8866): Max retries exceeded with url: http://localhost:8080/proxy?trustALLServers=true (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000273DE63C5C8>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it')))
Well, I had found that if the script will halt at some point without executing
server.stop()
it will mess with the system proxy settings by not clearing the changes it had made.