但是html和js写的代码还是有很大限制的,例如:不能更改本地文件。可是有一些还是很方便的。有系统,有浏览器就能使用。但是有些第三方js库必须要在服务器下菜能使用,所以我就写了一个本地测试服务器。
功能:
1,自动打开根目录下的inedx.html。
2,自动分配端口。
运行后的截图:

QQ_1746775628856.png (19.66 KB, 下载次数: 0)
下载附件
2025-5-9 15:27 上传
[color=]如果你要是喜欢,免费的评分给一个吧。
[Python] 纯文本查看 复制代码"""
本地测试服务器
功能:
1. 自动分配可用端口
2. 提供静态文件服务
"""
from flask import Flask, send_from_directory
from waitress import serve
import socket
import threading
import os
import sys
from colorama import init, Fore, Style
app = Flask(__name__)
# 获取本地IP地址
def get_local_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
s.close()
return ip
except:
return '127.0.0.1'
class LocalTestServer:
def __init__(self):
self.port = 0
# 判断是否是打包环境
if getattr(sys, 'frozen', False):
# 打包环境下使用可执行文件所在目录
self.root_path = os.path.dirname(sys.executable)
else:
# 开发环境下使用脚本所在目录
self.root_path = os.path.dirname(os.path.abspath(__file__))
self.server_thread = None
self.app = app
def find_available_port(self):
"""自动寻找可用端口"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 0))
return s.getsockname()[1]
def run_server(self):
"""启动WSGI服务器"""
self.port = self.find_available_port()
@app.route('/')
def index():
try:
return send_from_directory(self.root_path, 'index.html')
except:
return "未找到默认页面(index.html)", 404
@app.route('/')
def static_files(path):
return send_from_directory(self.root_path, path)
threading.Thread(
target=serve,
kwargs={
'app': app,
'port': self.port,
'threads': 4,
'host': '0.0.0.0'
}
).start()
# 等待服务器线程启动
while self.port == 0:
pass
def start(self):
"""启动服务器"""
if self.server_thread and self.server_thread.is_alive():
return False
self.server_thread = threading.Thread(target=self.run_server)
self.server_thread.daemon = True
self.server_thread.start()
return True
if __name__ == '__main__':
import webbrowser
server = LocalTestServer()
if server.start():
# 等待端口号被赋值
while server.port == 0:
pass
port = server.port
server.port = port # 确保端口号被正确赋值
init() # 初始化colorama
print(Fore.GREEN + '='*50 + Style.RESET_ALL)
print(Fore.CYAN + '本地测试服务器已启动' + Style.RESET_ALL)
print(Fore.GREEN + '='*50 + Style.RESET_ALL)
print(Fore.YELLOW + f'服务器状态: ' + Fore.GREEN + f'运行中 (PID: {os.getpid()})' + Style.RESET_ALL)
print(Fore.YELLOW + f'服务目录: ' + Fore.WHITE + f'{server.root_path}' + Style.RESET_ALL)
print(Fore.YELLOW + f'监听端口: ' + Fore.WHITE + f'{port}' + Style.RESET_ALL)
print(Fore.BLUE + '-'*50 + Style.RESET_ALL)
print(Fore.CYAN + '访问地址:' + Style.RESET_ALL)
print(Fore.YELLOW + '本机: ' + Fore.WHITE + f'http://localhost:{port}' + Style.RESET_ALL)
print(Fore.YELLOW + '局域网: ' + Fore.WHITE + f'http://{get_local_ip()}:{port}' + Style.RESET_ALL)
print(Fore.BLUE + '-'*50 + Style.RESET_ALL)
print(Fore.YELLOW + '按 ' + Fore.RED + 'Ctrl+C' + Fore.YELLOW + ' 停止服务器' + Style.RESET_ALL)
print(Fore.GREEN + '='*50 + Style.RESET_ALL)
# 自动打开浏览器
webbrowser.open(f'http://localhost:{port}')
try:
while True:
pass
except KeyboardInterrupt:
print(Fore.RED + '\n服务器已停止' + Style.RESET_ALL)
else:
print(Fore.RED + '服务器启动失败' + Style.RESET_ALL)
《Excel文件比较器v1.3》
https://www.52pojie.cn/thread-2020893-1-1.html
《个性计算器》
https://www.52pojie.cn/thread-2028540-1-1.html
没啥用软件 - 《Python库的批量安装》
https://www.52pojie.cn/thread-2030214-1-1.html