mirror of
https://github.com/internetarchive/brozzler.git
synced 2025-06-21 21:34:10 -04:00
Merge pull request #3 from internetarchive/AITFIVE-859
browser.py - Check for open ports before starting Chrome. Open next a…
This commit is contained in:
commit
07e15e26bd
1 changed files with 14 additions and 0 deletions
|
@ -34,6 +34,7 @@ from requests.structures import CaseInsensitiveDict
|
||||||
import select
|
import select
|
||||||
import re
|
import re
|
||||||
import base64
|
import base64
|
||||||
|
import psutil
|
||||||
|
|
||||||
__all__ = ["BrowserPool", "Browser"]
|
__all__ = ["BrowserPool", "Browser"]
|
||||||
|
|
||||||
|
@ -127,6 +128,7 @@ class Browser:
|
||||||
def start(self, proxy=None):
|
def start(self, proxy=None):
|
||||||
if not self._chrome_instance:
|
if not self._chrome_instance:
|
||||||
# these can raise exceptions
|
# these can raise exceptions
|
||||||
|
self.chrome_port = self._find_available_port()
|
||||||
self._work_dir = tempfile.TemporaryDirectory()
|
self._work_dir = tempfile.TemporaryDirectory()
|
||||||
self._chrome_instance = Chrome(port=self.chrome_port,
|
self._chrome_instance = Chrome(port=self.chrome_port,
|
||||||
executable=self.chrome_exe,
|
executable=self.chrome_exe,
|
||||||
|
@ -151,6 +153,18 @@ class Browser:
|
||||||
except:
|
except:
|
||||||
self.logger.error("problem stopping", exc_info=True)
|
self.logger.error("problem stopping", exc_info=True)
|
||||||
|
|
||||||
|
def _find_available_port(self):
|
||||||
|
port_available = False
|
||||||
|
port = self.chrome_port
|
||||||
|
|
||||||
|
for p in range(port,65535):
|
||||||
|
if any(connection.laddr[1] == p for connection in psutil.net_connections(kind='tcp')):
|
||||||
|
self.logger.warn("Port already open %s, will try %s", p, p + 1)
|
||||||
|
else:
|
||||||
|
port = p
|
||||||
|
break
|
||||||
|
return port
|
||||||
|
|
||||||
def is_running(self):
|
def is_running(self):
|
||||||
return bool(self._websocket_url)
|
return bool(self._websocket_url)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue