vcast_license_usage_server/server.py

191 lines
5.0 KiB
Python
Executable File

#!/usr/bin/python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import os
from pathlib import Path
sample_data = {
"27003@szmaslic03": {
"name": "27003@szmaslic03",
"total": 2,
"used": 1,
"used_info": [
{
"user": "haoxiang.ran",
"host": "szl3bc12808",
"lic": "szmaslic03",
"login": "0.41h"
}
]
},
"27003@szmaslic04": {
"name": "27003@szmaslic04",
"total": 2,
"used": 1,
"used_info": [
{
"user": "macro.yang",
"host": "szl3bc12804",
"lic": "szmaslic04",
"login": "52.28h"
}
]
},
"27003@szmaslic06": {
"name": "27003@szmaslic06",
"total": 2,
"used": 2,
"used_info": [
{
"user": "zhenhao.he",
"host": "szl3bc12804",
"lic": "szmaslic06",
"login": "0.20h"
},
{
"user": "phillip.chan",
"host": "szl3bc12810",
"lic": "szmaslic06",
"login": "0.16h"
}
]
},
"27003@szmaslic07": {
"name": "27003@szmaslic07",
"total": 2,
"used": 1,
"used_info": [
{
"user": "zhiyi.li",
"host": "szl3bc12806",
"lic": "szmaslic07",
"login": "2.96h"
}
]
},
"27003@szmaslic08": {
"name": "27003@szmaslic08",
"total": 2,
"used": 2,
"used_info": [
{
"user": "soo.liu",
"host": "szl3bc12809",
"lic": "szmaslic08",
"login": "6.55h"
},
{
"user": "haichao.ou",
"host": "szl3bc12806",
"lic": "szmaslic08",
"login": "5.93h"
}
]
},
"27003@szmaslic09": {
"name": "27003@szmaslic09",
"total": 2,
"used": 2,
"used_info": [
{
"user": "louie.liang",
"host": "szl3bc12810",
"lic": "szmaslic09",
"login": "8.31h"
},
{
"user": "zabbix",
"host": "szl3bc06409",
"lic": "szmaslic09",
"login": "0.90h"
}
]
},
"27003@szmaslic10": {
"name": "27003@szmaslic10",
"total": 2,
"used": 2,
"used_info": [
{
"user": "harvey.li",
"host": "szl3bc12810",
"lic": "szmaslic10",
"login": "57.38h"
},
{
"user": "kw.hu",
"host": "szl3bc12809",
"lic": "szmaslic10",
"login": "1.41h"
}
]
}
}
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
return # 重写该方法以禁用日志输出
def do_GET(self):
if self.path == '/vcast/lic_use_info':
self.send_response(200)
self.send_header('Content-type', 'text/json')
self.end_headers()
data = json.dumps(sample_data)
self.wfile.write(data.encode('utf-8'))
return
path = self.path
if path == '/':
path = '/index.html'
path = path.strip('/')
if not self.send_file(path):
self.send_404()
def send_404(self):
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"<h1>404 Not Found</h1><p>The page you are looking for does not exist.</p>")
def send_file(self, file_path) -> bool:
file_path =str(Path(os.path.abspath(__file__)).parent.joinpath(file_path))
print(f"clinet request: [{file_path}]")
if not os.path.isfile(file_path):
return False
self.send_response(200)
mime_type = self.get_mime_type(file_path)
self.send_header('Content-type', mime_type)
if mime_type.startswith('text/'):
self.send_header('Content-Disposition', f'inline; filename={os.path.basename(file_path)}')
else:
self.send_header('Content-Disposition', f'attachment; filename={os.path.basename(file_path)}')
self.end_headers()
with open(file_path, 'rb') as f:
self.wfile.write(f.read())
return True
def get_mime_type(self, file_path):
ext = os.path.splitext(file_path)[1]
if ext == '.txt':
return 'text/plain'
elif ext == '.js':
return 'application/javascript'
elif ext == '.html':
return 'text/html'
elif ext == '.css':
return 'text/css'
# 添加更多 MIME 类型根据需要
return 'application/octet-stream' # 默认类型
def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler, port=8088):
server_address = ('0.0.0.0', port)
httpd = server_class(server_address, handler_class)
print(f'Starting server on {server_address[0]}:{server_address[1]} ...')
httpd.serve_forever()
if __name__ == "__main__":
if len(os.sys.argv) > 1:
run(port=int(os.sys.argv[1]))
else:
run()