10 from http_server_driver import HTTPServerDriver
13 _log = logging.getLogger(__name__)
16 class SimpleHTTPServerDriver(HTTPServerDriver):
18 """This class depends on unix environment, need to be modified to achieve crossplatform compability
22 self.serverProcess = None
24 # FIXME: This may not be reliable.
25 _log.info('Finding the IP address of current machine')
27 self.ip = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][0]
28 _log.info('IP of current machine is: %s' % self.ip)
30 _log.error('Cannot get the ip address of current machine')
33 def serve(self, webroot):
34 oldWorkingDirectory = os.getcwd()
35 os.chdir(os.path.dirname(os.path.abspath(__file__)))
36 _log.info('Lauchning an http server')
37 self.serverProcess = subprocess.Popen(['/usr/bin/python', 'http_server/twisted_http_server.py', webroot], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
38 os.chdir(oldWorkingDirectory)
41 _log.info('Start to fetching the port number of the http server')
44 for attempt in xrange(maxAttempt):
46 self.serverPort = psutil.Process(self.serverProcess.pid).connections()[0][3][1]
48 _log.info('HTTP Server is serving at port: %d', self.serverPort)
52 _log.info('Server port is not found this time, retry after %f seconds' % interval)
57 for attempt in xrange(maxAttempt):
59 p = subprocess.Popen(' '.join(['/usr/sbin/lsof', '-a', '-iTCP', '-sTCP:LISTEN', '-p', str(self.serverProcess.pid)]), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
60 self.serverPort = int(re.findall('TCP \*:(\d+) \(LISTEN\)', p.communicate()[0])[0])
62 _log.info('HTTP Server is serving at port: %d', self.serverPort)
64 # Raising exception means the server is not ready to server, try later
69 _log.info('Server port is not found this time, retry after %f seconds' % interval)
73 raise Exception("Server may not be serving")
76 return "http://%s:%d" % (self.ip, self.serverPort)
78 def fetchResult(self):
79 return self.serverProcess.communicate()[0]