Python http.server.shutdown() Examples

The following are 9 code examples of http.server.shutdown(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module http.server , or try the search function .
Example #1
Source File: advancedhttpserver.py    From AdvancedHTTPServer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def finish_request(self, request, client_address):
		try:
			super(ServerNonThreaded, self).finish_request(request, client_address)
		except IOError:
			self.logger.warning('IOError encountered in finish_request')
		except KeyboardInterrupt:
			self.logger.warning('KeyboardInterrupt encountered in finish_request')
			self.shutdown() 
Example #2
Source File: advancedhttpserver.py    From AdvancedHTTPServer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def shutdown(self, *args, **kwargs):
		try:
			self.socket.shutdown(socket.SHUT_RDWR)
		except socket.error:
			pass
		self.socket.close() 
Example #3
Source File: advancedhttpserver.py    From AdvancedHTTPServer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def serve_forever(self, fork=False):
		"""
		Start handling requests. This method must be called and does not
		return unless the :py:meth:`.shutdown` method is called from
		another thread.

		:param bool fork: Whether to fork or not before serving content.
		:return: The child processes PID if *fork* is set to True.
		:rtype: int
		"""
		if fork:
			if not hasattr(os, 'fork'):
				raise OSError('os.fork is not available')
			child_pid = os.fork()
			if child_pid != 0:
				self.logger.info('forked child process: ' + str(child_pid))
				return child_pid
		self.__server_thread = threading.current_thread()
		self.__wakeup_fd = WakeupFd()
		self.__is_shutdown.clear()
		self.__should_stop.clear()
		self.__is_running.set()
		while not self.__should_stop.is_set():
			try:
				self._serve_ready()
			except socket.error:
				self.logger.warning('encountered socket error, stopping server')
				self.__should_stop.set()
		self.__is_shutdown.set()
		self.__is_running.clear()
		return 0 
Example #4
Source File: advancedhttpserver.py    From AdvancedHTTPServer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def shutdown(self):
		"""Shutdown the server and stop responding to requests."""
		self.__should_stop.set()
		if self.__server_thread == threading.current_thread():
			self.__is_shutdown.set()
			self.__is_running.clear()
		else:
			if self.__wakeup_fd is not None:
				os.write(self.__wakeup_fd.write_fd, b'\x00')
			self.__is_shutdown.wait()
		if self.__wakeup_fd is not None:
			self.__wakeup_fd.close()
			self.__wakeup_fd = None
		for server in self.sub_servers:
			server.shutdown() 
Example #5
Source File: advancedhttpserver.py    From AdvancedHTTPServer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def tearDown(self):
		if not self.shutdown_requested:
			self.assertTrue(self.server_thread.is_alive())
		self.http_connection.close()
		self.server.shutdown()
		self.server_thread.join(10.0)
		self.assertFalse(self.server_thread.is_alive())
		del self.server 
Example #6
Source File: registrar_common.py    From keylime with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def shutdown(self):
        http.server.HTTPServer.shutdown(self) 
Example #7
Source File: registrar_common.py    From keylime with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def shutdown(self):
        http.server.HTTPServer.shutdown(self) 
Example #8
Source File: registrar_common.py    From keylime with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def do_shutdown(servers):
    for server in servers:
        server.shutdown() 
Example #9
Source File: rasterize_test.py    From content with MIT License 5 votes vote down vote up
def http_wait_server():
    # Simple http handler which waits 10 seconds before responding
    class WaitHanlder(http.server.BaseHTTPRequestHandler):

        def do_HEAD(self):
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()

        def do_GET(self):
            time.sleep(10)
            try:
                self.send_response(200)
                self.send_header("Content-type", "text/html")
                self.end_headers()
                self.wfile.write(bytes("<html><head><title>Test wait handler</title></head>"
                                       "<body><p>Test Wait</p></body></html>", 'utf-8'))
                self.flush_headers()
            except BrokenPipeError:  # ignore broken pipe as socket might have been closed
                pass

        # disable logging

        def log_message(self, format, *args):
            pass

    with http.server.ThreadingHTTPServer(('', 10888), WaitHanlder) as server:
        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.start()
        yield
        server.shutdown()
        server_thread.join()


# Some web servers can block the connection after the http is sent
# In this case chromium will hang. An example for this is:
# curl -v -H 'user-agent: HeadlessChrome' --max-time 10  "http://www.grainger.com/"  # disable-secrets-detection
# This tests access a server which waits for 10 seconds and makes sure we timeout