【问题标题】:HTTPSimpleServer - How to close/terminate it?HTTP 简单服务器 - 如何关闭/终止它?
【发布时间】:2011-08-12 19:30:15
【问题描述】:

我最近了解到我可以使用以下命令运行服务器:

sudo python -m HTTPSimpleServer

我的问题:完成后如何终止此服务器?

【问题讨论】:

  • 旁注:您不必是 root 才能运行它(即不需要 sudo),因为它默认绑定到非特权端口。

标签: python macos terminal


【解决方案1】:

键入 Control-C。就这么简单。

【讨论】:

    【解决方案2】:

    您可能需要检查this servlet module 中的 HttpServer 类,以获取允许退出服务器的修改。如果处理程序引发 SystemExit 异常,服务器将中断其服务。


    class HttpServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
    
        """Create a server with specified address and handler.
    
        A generic web server can be instantiated with this class. It will listen
        on the address given to its constructor and will use the handler class
        to process all incoming traffic. Running a server is greatly simplified."""
    
        # We should not be binding to an
        # address that is already in use.
        allow_reuse_address = False
    
        @classmethod
        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 handle_error(self, request, client_address):
            """Process exceptions raised by the RequestHandlerClass.
    
            Overriding this method is necessary for two different reasons:
            (1) SystemExit exceptions are incorrectly caught otherwise and
            (2) Socket errors should be silently passed in the server code"""
            klass, value = sys.exc_info()[:2]
            if klass is SystemExit:
                self.__exit = value
                self._BaseServer__serving = None
            elif issubclass(klass, socket.error):
                pass
            else:
                super().handle_error(request, client_address)
    
        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
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多