我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用http.server.serve_forever()。
def main(): global server server = http.server.HTTPServer((host,port),HTTPRecquestHandler) print(time.asctime(),'Server started on port',port) print('....') print('ctrl-c to quit server.') try: server.serve_forever() if(close==True): raise #server.server_close() except KeyboardInterrupt: server.server_close() print(time.asctime(),"Server Stopped") except: server.server_close() print(time.asctime(),"Server Stopped")
def main(cls, RequestHandlerClass, port=80): """Start server with handler on given port. This static method provides an easy way to start, run, and exit a HttpServer instance. The server will be executed if possible, and the computer's web browser will be directed to the address.""" try: server = cls(('', port), RequestHandlerClass) active = True except socket.error: active = False else: addr, port = server.socket.getsockname() print('Serving HTTP on', addr, 'port', port, '...') finally: port = '' if port == 80 else ':' + str(port) addr = 'http://localhost' + port + '/' webbrowser.open(addr) if active: try: server.serve_forever() except KeyboardInterrupt: print('Keyboard interrupt received: EXITING') finally: server.server_close()
def serve_forever(self, poll_interval=0.5): """Handle all incoming client requests forever. This method has been overridden so that SystemExit exceptions raised in the RequestHandlerClass can be re-raised after being caught in the handle_error method above. This allows servlet code to terminate server execution if so desired or required.""" super().serve_forever(poll_interval) if self._BaseServer__serving is None: raise self.__exit
def serve_forever(self, content): self.stop = False self.content = content while not self.stop: self.handle_request()
def http_server(self, lhost, lport): print_warning("Setting up HTTP server") server = HttpServer((lhost, int(lport)), HttpRequestHandler) server.serve_forever(self.revshell) server.server_close()
def startThread(): server = ThreadedHTTPServer(('0.0.0.0', 1412), MyRequestHandler) print('Starting server, use <Ctrl-C> to stop') thread = threading.Thread(target = server.serve_forever) thread.deamon = False thread.start() print("--- WEB Server started (1235) ---") return server
def serve_forever(self): """Handle one request at a time until stopped.""" self.stop = False while not self.stop: self.handle_request() # stop the http server
def start_server(web_port, path): os.chdir(path) web_port = int(web_port) server = StoppableHttpServer(('', web_port), StoppableHttpRequestHandler) server.serve_forever()