• Python-使用tkinter实现的Django服务进程管理工具


      1 import tkinter
      2 import subprocess
      3 import os
      4 import time
      5 import re
      6 import sys
      7 from tkinter import Label, Button, StringVar
      8 from tkinter.messagebox import *
      9 
     10 
     11 MGR_FILE = "manage.py"
     12 MGR_DIR = "your Django project root directory" # Django项目根目录
     13 MGR_PATH = os.path.join(MGR_DIR, MGR_FILE)
     14 
     15 
     16 root = tkinter.Tk()
     17 setWidth, setHeight = root.maxsize()
     18 root.geometry('320x220+%d+%d' % ((setWidth-320)/2, (setHeight)/2-220))
     19 root.title('运行助手')
     20 root.resizable(width=False, height=False)
     21 
     22 
     23 def open_explo(url):
     24     subprocess.Popen('chrome %s' % url)
     25 
     26 
     27 def find_process():
     28     proc = subprocess.Popen('netstat -ano | findstr "8000"', shell=True, stdout=subprocess.PIPE).stdout.read()
     29     return proc
     30 
     31 
     32 def kill_process(res:str):
     33     try:
     34         pid_value = re.findall(r'LISTENINGs+?(d+)', res.decode())[0]
     35     except:
     36         if "TIME_WAIT" in res.decode():
     37             showwarning(title='提示信息', message='8000 端口未完全释放,请稍候重试。')
     38         else:
     39             showwarning(title='提示信息', message='Error: 未知错误')
     40         root.destroy()
     41         sys.exit(0)
     42     subprocess.Popen('taskkill /F /pid %s' % pid_value, shell=True, stdout=subprocess.PIPE)
     43 
     44 
     45 def check_btn():
     46     if bvar1.get()=="停止":
     47         button_index.config(state=tkinter.ACTIVE)
     48         button_admin.config(state=tkinter.ACTIVE)
     49     else:
     50         button_index.config(state=tkinter.DISABLED)
     51         button_admin.config(state=tkinter.DISABLED)
     52     root.update()
     53 
     54 
     55 def state_sw():
     56     if switch_btn['text'] != "停止":
     57         run_shell('python manage.py runserver')
     58         bvar1.set('停止')
     59         switch_btn['background'] = "#32A084"
     60         # showinfo(title='提示信息', message='开始运行')
     61         bottom_message['text'] = "开始运行"
     62         check_btn()
     63         time.sleep(0.5)
     64         bottom_message['text'] = "服务已启动"
     65     else:
     66         if askyesno('操作提示', '是否停止服务?', default='no'):
     67             search_res = find_process()
     68             if search_res:
     69                 kill_process(search_res)
     70                 bvar1.set('运行')
     71                 bottom_message['text'] = "停止服务"
     72                 check_btn()
     73                 switch_btn['background'] = "#EBEDEF"
     74                 time.sleep(0.5)
     75                 bottom_message['text'] = "就绪"
     76             else:
     77                 bottom_message['text'] = "未就绪"
     78                 showwarning(title='提示信息', message='服务进程不存在!')
     79                 bvar1.set('运行')
     80                 bottom_message['text'] = "就绪"
     81                 check_btn()
     82                 switch_btn['background'] = "#EBEDEF"
     83 
     84 
     85 def run_shell(run_param):
     86     mark = time.strftime('RA+%Y%m%d %H:%M:%S', time.localtime()) # 用于进程名称的特征字符串,方便过滤
     87     cmd = 'start run_assistant.bat "%s" %s' % (mark, run_param)
     88     console = subprocess.Popen(cmd, shell=True)
     89     if run_param == "python manage.py runserver":
     90         return
     91     root.withdraw()
     92     console.wait()
     93     while True:
     94         task_info = subprocess.Popen('tasklist /V | findstr /C:"%s"' % mark, shell=True, stdout=subprocess.PIPE)
     95         if not task_info.stdout.read():
     96             root.deiconify()
     97             break
     98 
     99 
    100 bvar1 = StringVar()
    101 bvar1.set('运行')
    102 
    103 label1 = Label(root, text='web服务',width=25,borderwidth=2,relief='groove',background='#f60',foreground='white')
    104 switch_btn = Button(root, textvariable=bvar1,background='#EBEDEF',command=state_sw)
    105 label1.grid(row=0,column=0,columnspan=5,padx=15,pady=10,ipadx=5,ipady=6)
    106 switch_btn.grid(row=0,column=5,padx=30,pady=10,ipadx=5,ipady=2)
    107 
    108 label2 = Label(root, text='管理终端',width=25,borderwidth=2,relief='groove',background='#f60',foreground='white')
    109 button2 = Button(root, text='运行',background='#EBEDEF',command=lambda:run_shell('python manage.py shell'))
    110 label2.grid(row=1,column=0,columnspan=5,padx=15,pady=10,ipadx=5,ipady=6)
    111 button2.grid(row=1,column=5,padx=30,pady=10,ipadx=5,ipady=2)
    112 
    113 label3 = Label(root, text='数据库终端',width=25,borderwidth=2,relief='groove',background='#f60',foreground='white')
    114 button3 = Button(root, text='运行',background='#EBEDEF',command=lambda:run_shell('python manage.py dbshell'))
    115 label3.grid(row=3,column=0,columnspan=5,padx=15,pady=10,ipadx=5,ipady=6)
    116 button3.grid(row=3,column=5,padx=30,pady=10,ipadx=5,ipady=2)
    117 
    118 button_index = Button(root, text='首页',command=lambda:open_explo('127.0.0.1:8000/index'))
    119 button_index.grid(row=4,column=3,padx=10,ipadx=5,ipady=2)
    120 button_admin = Button(root, text='控制台',command=lambda:open_explo('127.0.0.1:8000/admin'))
    121 button_admin.grid(row=4,column=4,ipady=2)
    122 
    123 bottom_message = Label(foreground='blue',width=36,anchor='w',font=('Arial', 8))
    124 bottom_message.grid(row=5,column=0,columnspan=6,padx=15,ipadx=5,sticky='W')
    125 
    126 ifSetup = find_process()
    127 check_btn()
    128 if ifSetup:
    129     root.withdraw()
    130     if askyesno(title='提示信息', message='8000 端口已被占用,是否帮您停止对应服务?'):
    131         kill_process(ifSetup)
    132         bottom_message['text'] = "就绪"
    133     else:
    134         switch_btn.config(state=tkinter.DISABLED)
    135         bottom_message['text'] = "未就绪"
    136     root.deiconify()
    137 
    138 
    139 if __name__ == '__main__':
    140     root.mainloop()

    run_assistant.bat文件:

    1 @echo off
    2 :: %pyenv% 为python虚拟环境根目录
    3 cd "%pyenv%Scripts"
    4 call activate.bat
    5 :: %project% 为Django项目根目录
    6 cd "%project%"
    7 :: %2 %3 %4 接收的3个参数为要执行的命令行语句
    8 %2 %3 %4

    界面截图:

  • 相关阅读:
    mybatis typeAlias (别名)说明
    jsp路径
    Jutil 单元测试
    SpringMVC数据绑定
    EL表达式(Exprission language)
    JSTL (标准标签库)
    Introduce Null Object
    Replace Type Code With Class和Replace Type Code With Subclass和Replace Type Code With State/Strategy
    利用QBuffer和QLinkedList做数据块存储队列
    Duplicate Observed Data
  • 原文地址:https://www.cnblogs.com/ywfft/p/a20200531.html
Copyright © 2020-2023  润新知