您的问题不明确-如果您通过shell(即python myscript.py)运行服务器,只需按crtl + C。
如果要使用代码优雅地关闭它,必须确定某个条件、点或异常才能将其调用shutdown。您可以添加一个块并调用httpd.shutdown()-因为The first class, HTTPServer, is a SocketServer.TCPServer subclass, and
therefore implements the SocketServer.BaseServer interface. It creates
and listens at the HTTP socket, dispatching the requests to a handler.
因此BaseServer有一个方法
例如:import os
from http.server import SimpleHTTPRequestHandler, HTTPServer
os.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')
server_address = ('', 8000)
try:
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
httpd.serve_forever()
except Exception:
httpd.shutdown()
有用的相关问题-