This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import http.server
import socketserver
import webbrowser
import os
PORT = 8205
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# 设定当前目录为服务器根目录
os.chdir(ROOT_DIR)
class Handler(http.server.SimpleHTTPRequestHandler):
def log_message(self, format, *args):
pass # 屏蔽日志输出(可选)
# 启动 HTTP 服务
def start_server():
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"访问地址:http://localhost:{PORT}/index.html")
webbrowser.open(f"http://localhost:{PORT}/index.html")
httpd.serve_forever()
if __name__ == "__main__":
start_server()