我有一个使用 Chat GPT(我不懂 Python)创建的 Python 代码来检查 URL 并下载 cmd 文件并启动它。
Chat GPT 创建的代码:
import os
import urllib.request
import subprocess
import win32api
import win32con
import ssl
import tkinter as tk
from tkinter import messagebox
ssl._create_default_https_context = ssl._create_unverified_context
# URL of the .cmd file
url = "https://github.com/OneDefauter/Menu_/releases/download/Run/Run.cmd"
# Directory where the file will be saved
dir_path = os.path.join(os.environ["HOMEDRIVE"], os.environ["HOMEPATH"])
# Creates the directory if it doesn't exist
if not os.path.exists(dir_path):
os.makedirs(dir_path)
# Full path of the file
file_path = os.path.join(dir_path, "TEMP---Run.cmd")
def run_script():
try:
# Checks the connection to the URL
urllib.request.urlopen(url)
except:
# If the connection fails, check if the file has already been downloaded
if os.path.exists(file_path):
subprocess.run(file_path, shell=True)
else:
root = tk.Tk()
root.withdraw()
messagebox.showerror(title="Error", message="URL unavailable and file not found.", parent=root)
root.after(3000, root.destroy) # closes the window after 3 seconds
# You can choose to display an error message using another GUI module or
# simply write to a log file or display a message on the command line
else:
# If the connection is successful, downloads and executes the file
try:
os.remove(os.path.join(dir_path, "TEMP---Run.cmd"))
except OSError:
pass
urllib.request.urlretrieve(url, file_path)
win32api.SetFileAttributes(file_path, win32con.FILE_ATTRIBUTE_HIDDEN)
subprocess.run(file_path, shell=True)
# Executes the function to download and execute the script
run_script()
我尝试使用 Chat GPT 将所有操作的完整日志部分放入运行的 cmd 文件中,但结果不是很令人满意。日志文件实际上已创建并在其中写入了一些内容,但终端一直是黑色的。
日志部分的代码:
import os
import urllib.request
import subprocess
import win32api
import win32con
import ssl
import tkinter as tk
from tkinter import messagebox
import datetime
ssl._create_default_https_context = ssl._create_unverified_context
# URL of the .cmd file
url = "https://github.com/OneDefauter/Menu_/releases/download/Run/Run.cmd"
# Directory where the file will be saved
dir_path = os.path.join(os.environ["HOMEDRIVE"], os.environ["HOMEPATH"])
# Create directory if it doesn't exist
if not os.path.exists(dir_path):
os.makedirs(dir_path)
# Log file name
log_filename = "Log_{}.log".format(datetime.datetime.now().strftime("%H-%M-%S_%d_%m_%Y"))
# Full log file path
log_file_path = os.path.join(dir_path, log_filename)
# Full path of the file
file_path = os.path.join(dir_path, "TEMP---Run.cmd")
def run_script():
try:
# Check connection to the URL
urllib.request.urlopen(url)
except:
# If connection fails, check if the file has already been downloaded
if os.path.exists(file_path):
with open(log_file_path, 'a', encoding='utf-8') as log_file:
subprocess.run(file_path, shell=True, stdout=log_file, stderr=log_file)
else:
root = tk.Tk()
root.withdraw()
messagebox.showerror(title="Error", message="Unavailable URL and file not found.", parent=root)
root.after(3000, root.destroy) # close the window after 3 seconds
# You can choose to display an error message using another GUI module or
# simply write to a log file or display a message on the command line
else:
# If the connection is successful, download and execute the file
try:
os.remove(file_path)
except OSError:
pass
urllib.request.urlretrieve(url, file_path)
win32api.SetFileAttributes(file_path, win32con.FILE_ATTRIBUTE_HIDDEN)
with open(log_file_path, 'a', encoding='utf-8') as log_file:
subprocess.run(file_path, shell=True, stdout=log_file, stderr=log_file)
# Execute the function to download and run the script
run_script()