-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@echo off | ||
title ENoW builder | ||
pyinstaller --onefile main.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"nginx_directory": "D:/Toolbox/nginx" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import os | ||
import subprocess | ||
import time | ||
|
||
|
||
def status(): | ||
result = subprocess.run( | ||
["tasklist", "/FI", "IMAGENAME eq nginx.exe"], capture_output=True, text=True | ||
) | ||
if "nginx.exe" in result.stdout: | ||
return "running" | ||
else: | ||
return "stopped" | ||
|
||
|
||
# 1 | ||
def start(nginx_directory): | ||
os.chdir(nginx_directory) | ||
subprocess.Popen(["nginx"], capture_output=True, text=True) | ||
|
||
|
||
# 2 | ||
def stop(nginx_directory): | ||
os.chdir(nginx_directory) | ||
if status() == "running": | ||
subprocess.run(["nginx", "-s", "stop"], capture_output=True, text=True) | ||
time.sleep(0.5) | ||
|
||
|
||
# 3 | ||
def quit(nginx_directory): | ||
os.chdir(nginx_directory) | ||
if status() == "running": | ||
subprocess.run(["nginx", "-s", "quit"], capture_output=True, text=True) | ||
time.sleep(0.5) | ||
|
||
|
||
# 4 | ||
def taskkill(): | ||
if status() == "running": | ||
subprocess.run( | ||
["taskkill", "/F", "/IM", "nginx.exe"], capture_output=True, text=True | ||
) | ||
|
||
|
||
# 5 | ||
def reload(nginx_directory): | ||
os.chdir(nginx_directory) | ||
if status() == "running": | ||
subprocess.run(["nginx", "-s", "reload"], capture_output=True, text=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import json | ||
import os | ||
from colorama import init, Fore, Style | ||
|
||
import control_nginx | ||
|
||
init(autoreset=True) | ||
|
||
|
||
def load_config(): | ||
try: | ||
with open("config.json", "r") as f: | ||
config = json.load(f) | ||
print("配置加载成功。") | ||
return config | ||
except json.JSONDecodeError as e: | ||
print(f"读取JSON文件时出错:{e}") | ||
return None | ||
except FileNotFoundError as e: | ||
print(f"未找到配置文件:{e}") | ||
return None | ||
|
||
|
||
def check_nginx_executable(nginx_directory): | ||
nginx_exe_path = os.path.join(nginx_directory, "nginx.exe") | ||
if os.path.isfile(nginx_exe_path): | ||
return True | ||
else: | ||
print(f"未找到nginx.exe在目录:{nginx_directory}") | ||
return False | ||
|
||
|
||
def main(): | ||
config = load_config() | ||
if not config: | ||
print("加载配置失败。") | ||
return | ||
|
||
nginx_directory = config["nginx_directory"] | ||
print(f"Nginx目录:{nginx_directory}") | ||
|
||
if not check_nginx_executable(nginx_directory): | ||
return | ||
|
||
while True: | ||
|
||
nginx_status = control_nginx.status() | ||
if "running" in nginx_status: | ||
status_colored = Fore.GREEN + nginx_status + Style.RESET_ALL | ||
else: | ||
status_colored = Fore.RED + nginx_status + Style.RESET_ALL | ||
|
||
os.system("cls") | ||
print("\nEasy Nginx on Windows") | ||
print(f"当前状态:{status_colored}") | ||
print("0. 刷新") | ||
print("1. 启动Nginx") | ||
print("2. 停止Nginx(快速)") | ||
print("3. 退出Nginx(标准)") | ||
print("4. 强制停止Nginx") | ||
print("5. 重新加载Nginx") | ||
print("6. 退出ENoW") | ||
choice = input("请输入你的选择:") | ||
|
||
if choice == "1": | ||
control_nginx.start(nginx_directory) | ||
elif choice == "2": | ||
control_nginx.stop(nginx_directory) | ||
elif choice == "3": | ||
control_nginx.quit(nginx_directory) | ||
elif choice == "4": | ||
control_nginx.taskkill() | ||
elif choice == "5": | ||
control_nginx.reload(nginx_directory) | ||
elif choice == "6": | ||
break | ||
else: | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |