1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
import socket
import subprocess
import time
import os
def is_connectable(host, port):
sock = None
try:
sock = socket.create_connection((host, port), 1)
result = True
except socket.error:
result = False
finally:
if sock:
sock.close()
return result
def resolve_path(path):
return os.path.normpath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), path))
class ProxyServer(object):
def __init__(self, binary_path, config_path):
self.process = subprocess.Popen([binary_path, config_path],
shell=False)
def wait_until_connectable(self, host, port, timeout=10):
count = 0
while not is_connectable(host=host, port=port):
if self.process.poll() is not None:
# process has exited
raise Exception(
'The process appears to have exited '
'before we could connect.')
if count >= timeout:
self.kill()
raise Exception(
'The proxy server has not binded '
'to (%s, %s) in %d seconds'
% (host, port, timeout))
count += 1
time.sleep(1)
return True
def kill(self):
if self.process:
self.process.terminate()
self.process.kill()
self.process.wait()
|