def wsgi_server_host_port(request): wsgi_args = dict(request.config.option.wsgi_args or ()) if ('port_range.min' in wsgi_args and 'port_range.max' in wsgi_args): import socket import os # return available port in specified range if min and max are defined port_temp, port_max = int(wsgi_args['port_range.min']), int( wsgi_args['port_range.max']) port_assigned = False s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) while not port_assigned and port_temp <= port_max: try: s.bind(('', port_temp)) port_assigned = True except OSError: port_temp += 1 if not port_assigned: # port failed to be assigned, so raise an error raise ip, port = s.getsockname() s.close() ip = os.environ.get('WEBTEST_SERVER_BIND', '127.0.0.1') return ip, port else: # otherwise get any free port from webtest.http import get_free_port return get_free_port()
def _run_server(self, app): """Run a wsgi server in a separate thread""" ip, port = get_free_port() self.app = app = WSGIApplication(app, (ip, port)) def run(): logger = logging.getLogger("SeleniumWebDriverApp") def log_message(self, format, *args): logger.info("%s - - [%s] %s\n" % (self.address_string(), self.log_date_time_string(), format % args)) # monkey patch to redirect request handler logs WSGIRequestHandler.log_message = log_message httpd = simple_server.make_server(ip, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler) httpd.serve_forever() app.thread = Process(target=run) app.thread.start() conn = HTTPConnection(ip, port) time.sleep(.5) for i in range(100): try: conn.request('GET', '/__application__') conn.getresponse() except (socket.error, CannotSendRequest): time.sleep(.3) else: break
def wsgi_server_host_port(request): wsgi_args = dict(request.config.option.wsgi_args or ()) if ('port_range.min' in wsgi_args and 'port_range.max' in wsgi_args): import socket import os # return available port in specified range if min and max are defined port_temp, port_max = int(wsgi_args['port_range.min']), int(wsgi_args['port_range.max']) port_assigned = False s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) while not port_assigned and port_temp <= port_max: try: s.bind(('', port_temp)) port_assigned = True except OSError: port_temp += 1 if not port_assigned: # port failed to be assigned, so raise an error raise ip, port = s.getsockname() s.close() ip = os.environ.get('WEBTEST_SERVER_BIND', '127.0.0.1') return ip, port else: # otherwise get any free port from webtest.http import get_free_port return get_free_port()
def create(cls, application, **kwargs): """Start a server to serve ``application``. Return a server instance.""" host, port = get_free_port() kwargs['port'] = port if 'host' not in kwargs: kwargs['host'] = host if 'expose_tracebacks' not in kwargs: kwargs['expose_tracebacks'] = True server = cls(application, **kwargs) server.runner = threading.Thread(target=server.run) server.runner.daemon = True server.runner.start() return server
def _testapp_process_host_port(tmpdir, testapp_file): """Execute watcher in subprocess; yield (popen-obj, host, port).""" host, port = get_free_port() os.chdir(str(tmpdir)) process = Popen( [ sys.executable, '-c', ( "from wsgiwatcher import watcher; " "watcher.run('testapp.serve_forever', 1)" ), ], env={ TESTAPP_HOST_ENVVAR: host, TESTAPP_PORT_ENVVAR: str(port), 'PYTHONDONTWRITEBYTECODE': '1', }, ) yield (process, host, port) # Only shut down if we haven't been shut down already. if process.poll() is None: process.terminate() process.wait()
def elasticsearch_host_port(): from webtest.http import get_free_port return get_free_port()
def server_host_port(): from webtest.http import get_free_port return get_free_port()
def test_no_server(self): host, port = http.get_free_port() self.assertEqual(0, http.check_server(host, port, retries=2))
def test_shutdown_non_running(self): host, port = http.get_free_port() s = http.StopableWSGIServer(debug_app, host=host, port=port) self.assertFalse(s.wait(retries=1)) self.assertTrue(s.shutdown())
def wsgi_server_host_port(): from webtest.http import get_free_port return get_free_port()
def callFTU(self, **kw): kw['host'], kw['port'] = http.get_free_port() s = aiowsgi.create_server(self.app, threads=1, loop=self.loop, **kw).proto() s.connection_made(Transport()) return s
def test_shutdown_non_running(self): host, port = http.get_free_port() s = http.StopableWSGIServer(debug_app, host=host, port=port) self.assertFalse(s.wait(retries=-1)) self.assertTrue(s.shutdown())