添加 host_ollama.py
This commit is contained in:
parent
70033b34ad
commit
9cc7e1f454
113
host_ollama.py
Normal file
113
host_ollama.py
Normal file
@ -0,0 +1,113 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Ollama 本地镜像服务器
|
||||
功能:
|
||||
1. 自动检测并Host下载的镜像目录
|
||||
2. 提供文件浏览和下载接口
|
||||
3. 自动生成友好的下载页面
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
||||
from pathlib import Path
|
||||
import threading
|
||||
import webbrowser
|
||||
|
||||
MIRROR_DIR = "ollama_releases" # 与mirror_ollama.sh保持一致
|
||||
PORT = 8000
|
||||
|
||||
class OllamaRequestHandler(SimpleHTTPRequestHandler):
|
||||
"""自定义请求处理器,增强目录浏览功能"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, directory=MIRROR_DIR, **kwargs)
|
||||
|
||||
def list_directory(self, path):
|
||||
"""生成增强型目录列表页面"""
|
||||
try:
|
||||
items = []
|
||||
for item in Path(path).iterdir():
|
||||
mtime = item.stat().st_mtime
|
||||
size = item.stat().st_size
|
||||
items.append((item.name, mtime, size))
|
||||
|
||||
# 按版本号排序(降序)
|
||||
items.sort(key=lambda x: x[0], reverse=True)
|
||||
|
||||
# 生成HTML
|
||||
html = ["<html><head><title>Ollama 本地镜像</title>"]
|
||||
html.append("<style>")
|
||||
html.append("body { font-family: Arial, sans-serif; margin: 20px; }")
|
||||
html.append("table { border-collapse: collapse; width: 100%; }")
|
||||
html.append("th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; }")
|
||||
html.append("tr:hover { background-color: #f5f5f5; }")
|
||||
html.append("</style></head><body>")
|
||||
html.append("<h1>Ollama 本地镜像库</h1>")
|
||||
html.append(f"<p>当前目录: {path}</p>")
|
||||
html.append("<table><tr><th>文件名</th><th>大小</th><th>修改时间</th></tr>")
|
||||
|
||||
for name, mtime, size in items:
|
||||
human_size = f"{size/1024/1024:.1f} MB" if size > 1024*1024 else f"{size/1024:.1f} KB"
|
||||
html.append(
|
||||
f'<tr><td><a href="{name}">{name}</a></td>'
|
||||
f'<td>{human_size}</td>'
|
||||
f'<td>{mtime:.0f}</td></tr>'
|
||||
)
|
||||
|
||||
html.append("</table></body></html>")
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/html; charset=utf-8")
|
||||
self.end_headers()
|
||||
self.wfile.write("\n".join(html).encode('utf-8'))
|
||||
|
||||
except Exception as e:
|
||||
self.send_error(500, f"服务器错误: {str(e)}")
|
||||
|
||||
def start_server(port=PORT):
|
||||
"""启动HTTP服务器"""
|
||||
server_address = ('', port)
|
||||
httpd = HTTPServer(server_address, OllamaRequestHandler)
|
||||
|
||||
print(f"\n🎯 Ollama 本地镜像服务已启动")
|
||||
print(f"👉 访问地址: http://localhost:{port}")
|
||||
print(f"📁 镜像目录: {os.path.abspath(MIRROR_DIR)}")
|
||||
print("🛑 按 Ctrl+C 停止服务\n")
|
||||
|
||||
try:
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
print("\n服务器已停止")
|
||||
|
||||
def ensure_mirror_dir():
|
||||
"""确保镜像目录存在"""
|
||||
if not os.path.exists(MIRROR_DIR):
|
||||
os.makedirs(MIRROR_DIR)
|
||||
print(f"创建镜像目录: {MIRROR_DIR}")
|
||||
return False
|
||||
return True
|
||||
|
||||
def open_browser():
|
||||
"""自动打开浏览器"""
|
||||
import time
|
||||
time.sleep(1)
|
||||
webbrowser.open(f"http://localhost:{PORT}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 检查并获取端口参数
|
||||
port = PORT
|
||||
if len(sys.argv) > 1:
|
||||
try:
|
||||
port = int(sys.argv[1])
|
||||
except ValueError:
|
||||
print("错误: 端口号必须是数字")
|
||||
sys.exit(1)
|
||||
|
||||
if not ensure_mirror_dir():
|
||||
print("⚠️ 注意: 镜像目录为空,请先运行 mirror_ollama.sh 下载文件")
|
||||
|
||||
# 自动打开浏览器
|
||||
threading.Thread(target=open_browser, daemon=True).start()
|
||||
|
||||
# 启动服务器
|
||||
start_server(port)
|
||||
Loading…
Reference in New Issue
Block a user