def _run_wsgi(self, addr, port, wsgi_handler): server_address = (addr, port) httpd = WSGIServer(server_address, WSGIRequestHandler) httpd.set_app(wsgi_handler) #httpd.serve_forever() while not self.stop_requested: httpd.handle_request()
def run(self): self.lock.acquire() pidfile = os.path.join(tempfile.gettempdir(), 'lettuce-django.pid') if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, 9) except OSError: pass finally: os.unlink(pidfile) open(pidfile, 'w').write(unicode(os.getpid())) bound = False max_port = 65535 connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM) while not bound or self.port < max_port: try: connector.connect((self.address, self.port)) self.port += 1 except socket.error: bound = True break if bound: try: server_address = (self.address, self.port) httpd = WSGIServer(server_address, MutedRequestHandler) bound = True except WSGIServerException: bound = False if not bound: raise LettuceServerException( "the port %d already being used, could not start " \ "django's builtin server on it" % self.port ) handler = StopabbleHandler(WSGIHandler()) if 'django.contrib.admin' in settings.INSTALLED_APPS: admin_media_path = '' handler = AdminMediaHandler(handler, admin_media_path) print "Preparing to server django's admin site static files..." httpd.set_app(handler) global keep_running while keep_running: call_hook('before', 'handle_request', httpd, self) httpd.handle_request() call_hook('after', 'handle_request', httpd, self) if self.lock.locked(): self.lock.release()
def run(self): """ Sets up the live server and databases, and then loops over handling http requests. """ if self.connections_override: # Override this thread's database connections with the ones # provided by the main thread. for alias, conn in self.connections_override.items(): connections[alias] = conn try: # Create the handler for serving static and media files handler = self.static_handler(_MediaFilesHandler(WSGIHandler())) # Go through the list of possible ports, hoping that we can find # one that is free to use for the WSGI server. for index, port in enumerate(self.possible_ports): try: if self.use_ssl: self.httpd = WSGIServer( (self.host, port), SecureWSGIRequestHandler) self.httpd.socket = self.wrap_socket(self.httpd.socket) else: self.httpd = WSGIServer( (self.host, port), QuietWSGIRequestHandler) except socket.error as e: if (index + 1 < len(self.possible_ports) and e.errno == errno.EADDRINUSE): # This port is already in use, so we go on and try with # the next one in the list. continue else: # Either none of the given ports are free or the error # is something else than "Address already in use". So # we let that error bubble up to the main thread. raise else: # A free port was found. self.port = port break self.httpd.set_app(handler) self.is_ready.set() self.httpd.serve_forever() except Exception as e: self.error = e self.is_ready.set()
def _create_server(self): """ the keep-alive fixes introduced in Django 2.1.4 (934acf1126995f6e6ccba5947ec8f7561633c27f) cause problems when serving the static files in a stream. We disable the helper handle method that calls handle_one_request multiple times. """ QuietWSGIRequestHandler.handle = QuietWSGIRequestHandler.handle_one_request return WSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False)
def run(self): """ Sets up the live server and databases, and then loops over handling http requests. """ if self.connections_override: # Override this thread's database connections with the ones # provided by the main thread. for alias, conn in self.connections_override.items(): connections[alias] = conn try: # Create the handler for serving static and media files handler = self.static_handler(_MediaFilesHandler(WSGIHandler())) # Go through the list of possible ports, hoping that we can find # one that is free to use for the WSGI server. for index, port in enumerate(self.possible_ports): try: self.httpd = WSGIServer( (self.host, port), QuietWSGIRequestHandler) except WSGIServerException as e: if (index + 1 < len(self.possible_ports) and hasattr(e.args[0], 'errno') and e.args[0].errno == errno.EADDRINUSE): # This port is already in use, so we go on and try with # the next one in the list. continue else: # Either none of the given ports are free or the error # is something else than "Address already in use". So # we let that error bubble up to the main thread. raise else: # A free port was found. self.port = port break self.httpd.set_app(handler) self.is_ready.set() self.httpd.serve_forever() except Exception as e: self.error = e self.is_ready.set()
def test_broken_pipe_errors(self): """WSGIServer handles broken pipe errors.""" request = WSGIRequest(self.request_factory.get("/").environ) client_address = ("192.168.2.0", 8080) msg = f"- Broken pipe from {client_address}" tests = [ BrokenPipeError, ConnectionAbortedError, ConnectionResetError, ] for exception in tests: with self.subTest(exception=exception): try: server = WSGIServer(("localhost", 0), WSGIRequestHandler) try: raise exception() except Exception: with captured_stderr() as err: with self.assertLogs("django.server", "INFO") as cm: server.handle_error(request, client_address) self.assertEqual(err.getvalue(), "") self.assertEqual(cm.records[0].getMessage(), msg) finally: server.server_close()
def run(self): self.lock.acquire() pidfile = os.path.join(tempfile.gettempdir(), 'lettuce-django.pid') if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, 9) except OSError: pass finally: os.unlink(pidfile) open(pidfile, 'w').write(unicode(os.getpid())) bound = False max_port = 65535 while not bound or self.port > max_port: try: server_address = (self.address, self.port) httpd = WSGIServer(server_address, MutedRequestHandler) bound = True except WSGIServerException: self.port += 1 if not bound: raise LettuceServerException( "the port %d already being used, could not start " \ "django's builtin server on it" % self.port ) httpd.set_app(StopabbleHandler(WSGIHandler())) self.lock.release() global keep_running while keep_running: httpd.handle_request()
def run(self): try: handler = AdminMediaHandler(WSGIHandler()) server_address = (config.HOST, config.PORT) httpd = WSGIServer(server_address, SilentWSGIRequestHandler) httpd.set_app(handler) while self.running.isSet(): httpd.handle_request() httpd.server_close() except WSGIServerException, e: pass
def run(self): self.lock.acquire() pidfile = os.path.join(tempfile.gettempdir(), 'lettuce-django.pid') if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, 9) except OSError: pass finally: os.unlink(pidfile) open(pidfile, 'w').write(unicode(os.getpid())) connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s = connector.connect((self.address, self.port)) self.lock.release() os.kill(os.getpid(), 9) except socket.error: pass finally: self.lock.release() try: server_address = (self.address, self.port) httpd = WSGIServer(server_address, MutedRequestHandler) except WSGIServerException: raise LettuceServerException( "the port %d already being used, could not start " \ "django's builtin server on it" % self.port, ) handler = WSGIHandler() if self.should_serve_admin_media(): admin_media_path = '' handler = AdminMediaHandler(handler, admin_media_path) if self.should_serve_static_files(): handler = StaticFilesHandler(handler) httpd.set_app(handler) global keep_running while keep_running: call_hook('before', 'handle_request', httpd, self) httpd.handle_request() call_hook('after', 'handle_request', httpd, self) try: self.lock.release() except ValueError: pass
def run_wsgi_server(self, addr, port, handler): """ replaces ``django.core.servers.basehttp.run`` """ global PERSISTENT_SOCK kwargs = dict(bind_and_activate=False) if self.has_ipv6_support: kwargs['ipv6'] = self.use_ipv6 httpd = WSGIServer((addr, port), WSGIRequestHandler, **kwargs) # patch the socket httpd.socket = PERSISTENT_SOCK try: httpd.server_bind() except WSGIServerException, e: if 'Errno 22' in str(e): # may have been bound, just emulate some stuff done in server_bind (like setting up environ) httpd.server_name = socket.getfqdn(addr) httpd.server_port = port httpd.setup_environ() else: raise
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") from colors import add_markup from django.core.servers.basehttp import WSGIServer, WSGIRequestHandler, get_internal_wsgi_application logpath = os.getenv('PYTHON_SERVICE_ARGUMENT') class RequestHandler(WSGIRequestHandler): def log_message(self, format, *args): # Don't bother logging requests for admin images, or the favicon. if (self.path.startswith(self.admin_static_prefix) or self.path == '/favicon.ico'): return msg = "[%s] %s" % (self.log_date_time_string(), format % args) kivymarkup = add_markup(msg, args) with open(logpath, 'a') as fh: fh.write(kivymarkup + '\n') fh.flush() server_address = ('0.0.0.0', 8000) wsgi_handler = get_internal_wsgi_application() httpd = WSGIServer(server_address, RequestHandler) httpd.set_app(wsgi_handler) httpd.serve_forever()
def server_bind(self): """Sets timeout to 1 second.""" WSGIServer.server_bind(self) self.socket.settimeout(1)
def __init__(self, *args, **kwargs): WSGIServer.__init__(self, *args, **kwargs) self.__is_shut_down = _ImprovedEvent() self.__serving = False
def serve(view, host='localhost', port=6789): httpd = WSGIServer((host, port), WSGIRequestHandler) httpd.set_app(WSGIWrapper(view)) httpd.serve_forever()
def run(application, addr, port): httpd = WSGIServer((addr, port), WSGIRequestHandler, ipv6=False) httpd.set_app(application) httpd.serve_forever()
def handle_request(self, timeout=1.0): reads, writes, errors = (self, ), (), () reads, writes, errors = select.select(reads, writes, errors, timeout) if reads: WSGIServer.handle_request(self)
def __init__(self, *args, **kwargs): WSGIServer.__init__(self, *args, **kwargs)
def __init__(self, *args, **kwargs): WSGIServer.__init__(self, *args, **kwargs) self.__is_shut_down = _ImprovedEvent() self.__serving = False path = '/opt/yum-repo-server'
class LiveServerThread(threading.Thread): """ Thread for running a live http server while the tests are running. """ def __init__(self, host, possible_ports, static_handler, connections_override=None): self.host = host self.port = None self.possible_ports = possible_ports self.is_ready = threading.Event() self.error = None self.static_handler = static_handler self.connections_override = connections_override super(LiveServerThread, self).__init__() def run(self): """ Sets up the live server and databases, and then loops over handling http requests. """ if self.connections_override: # Override this thread's database connections with the ones # provided by the main thread. for alias, conn in self.connections_override.items(): connections[alias] = conn try: # Create the handler for serving static and media files handler = self.static_handler(_MediaFilesHandler(WSGIHandler())) # Go through the list of possible ports, hoping that we can find # one that is free to use for the WSGI server. for index, port in enumerate(self.possible_ports): try: self.httpd = WSGIServer( (self.host, port), QuietWSGIRequestHandler) except socket.error as e: if (index + 1 < len(self.possible_ports) and e.errno == errno.EADDRINUSE): # This port is already in use, so we go on and try with # the next one in the list. continue else: # Either none of the given ports are free or the error # is something else than "Address already in use". So # we let that error bubble up to the main thread. raise else: # A free port was found. self.port = port break self.httpd.set_app(handler) self.is_ready.set() self.httpd.serve_forever() except Exception as e: self.error = e self.is_ready.set() def terminate(self): if hasattr(self, 'httpd'): # Stop the WSGI server self.httpd.shutdown() self.httpd.server_close()
def _create_server(self): return WSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False)
def run(self): self.lock.acquire() pidfile = os.path.join(tempfile.gettempdir(), "lettuce-django.pid") if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, 9) except OSError: pass finally: os.unlink(pidfile) open(pidfile, "w").write(unicode(os.getpid())) self.configure_mail_queue() connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s = connector.connect((self.address, self.port)) self.lock.release() os.kill(os.getpid(), 9) except socket.error: pass finally: self.lock.release() try: server_address = (self.address, self.port) httpd = WSGIServer(server_address, MutedRequestHandler) except socket.error: raise LettuceServerException( "the port %d already being used, could not start " "django's builtin server on it" % self.port ) handler = WSGIHandler() if self.should_serve_admin_media(): if not AdminMediaHandler: raise LettuceServerException( "AdminMediaHandler is not available in this version of " "Django. Please set LETTUCE_SERVE_ADMIN_MEDIA = False " "in your Django settings." ) admin_media_path = "" handler = AdminMediaHandler(handler, admin_media_path) if self.should_serve_static_files(): handler = StaticFilesHandler(handler) httpd.set_app(handler) global keep_running while keep_running: call_hook("before", "handle_request", httpd, self) httpd.handle_request() call_hook("after", "handle_request", httpd, self) try: self.lock.release() except ValueError: pass
def run(self): self.lock.acquire() pidfile = os.path.join(tempfile.gettempdir(), "lettuce-django.pid") if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, 9) except OSError: pass finally: os.unlink(pidfile) open(pidfile, "w").write(unicode(os.getpid())) bound = False max_port = 65535 connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM) while not bound or self.port < max_port: try: connector.connect((self.address, self.port)) self.port += 1 except socket.error: bound = True break if bound: try: server_address = (self.address, self.port) httpd = WSGIServer(server_address, MutedRequestHandler) bound = True except WSGIServerException: bound = False if not bound: raise LettuceServerException( "the port %d already being used, could not start " "django's builtin server on it" % self.port ) handler = WSGIHandler() if self.should_serve_admin_media(): admin_media_path = "" handler = AdminMediaHandler(handler, admin_media_path) if self.should_serve_static_files(): handler = StaticFilesHandler(handler) httpd.set_app(handler) global keep_running while keep_running: call_hook("before", "handle_request", httpd, self) httpd.handle_request() call_hook("after", "handle_request", httpd, self) try: self.lock.release() except ValueError: pass