Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check to see if Server is Running Before Starting New #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion browsermobproxy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def __init__(self, url, params=None, options=None):
jcontent = json.loads(content)
except Exception as e:
raise Exception("Could not read Browsermob-Proxy json\n"
"Another server running on this port?\n%s..." % content[:512])
"Another server running on this port?\n%s..."%content[:512])

self.port = jcontent['port']
url_parts = self.host.split(":")
self.proxy = url_parts[1][2:] + ":" + str(self.port)
Expand Down
10 changes: 8 additions & 2 deletions browsermobproxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import socket
import subprocess
import time
import logging

import sys

Expand Down Expand Up @@ -43,10 +44,10 @@ def create_proxy(self, params=None):
client = Client(self.url[7:], params)
return client

def _is_listening(self):
def _is_listening(self, timeout = 1):
try:
socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_.settimeout(1)
socket_.settimeout(timeout)
socket_.connect((self.host, self.port))
socket_.close()
return True
Expand Down Expand Up @@ -113,6 +114,11 @@ def start(self, options=None):
log_path_name = os.path.join(log_path, log_file)
self.log_file = open(log_path_name, 'w')


if self._is_listening(.1):
logging.info("BrowserMob proxy already running. Won't start again.")
return

if self.win_env:
self.process = self._start_on_windows()
else:
Expand Down