Python-使用tkinter实现的Django服务进程管理工具
import tkinter
import subprocess
import os
import time
import re
import sys
from tkinter import Label, Button, StringVar
from tkinter.messagebox import * MGR_FILE = "manage.py"
MGR_DIR = "your Django project root directory" # Django项目根目录
MGR_PATH = os.path.join(MGR_DIR, MGR_FILE) root = tkinter.Tk()
setWidth, setHeight = root.maxsize()
root.geometry('320x220+%d+%d' % ((setWidth-320)/2, (setHeight)/2-220))
root.title('运行助手')
root.resizable(width=False, height=False) def open_explo(url):
subprocess.Popen('chrome %s' % url) def find_process():
proc = subprocess.Popen('netstat -ano | findstr "8000"', shell=True, stdout=subprocess.PIPE).stdout.read()
return proc def kill_process(res:str):
try:
pid_value = re.findall(r'LISTENING\s+?(\d+)', res.decode())[0]
except:
if "TIME_WAIT" in res.decode():
showwarning(title='提示信息', message='8000 端口未完全释放,请稍候重试。')
else:
showwarning(title='提示信息', message='Error: 未知错误')
root.destroy()
sys.exit(0)
subprocess.Popen('taskkill /F /pid %s' % pid_value, shell=True, stdout=subprocess.PIPE) def check_btn():
if bvar1.get()=="停止":
button_index.config(state=tkinter.ACTIVE)
button_admin.config(state=tkinter.ACTIVE)
else:
button_index.config(state=tkinter.DISABLED)
button_admin.config(state=tkinter.DISABLED)
root.update() def state_sw():
if switch_btn['text'] != "停止":
run_shell('python manage.py runserver')
bvar1.set('停止')
switch_btn['background'] = "#32A084"
# showinfo(title='提示信息', message='开始运行')
bottom_message['text'] = "开始运行"
check_btn()
time.sleep(0.5)
bottom_message['text'] = "服务已启动"
else:
if askyesno('操作提示', '是否停止服务?', default='no'):
search_res = find_process()
if search_res:
kill_process(search_res)
bvar1.set('运行')
bottom_message['text'] = "停止服务"
check_btn()
switch_btn['background'] = "#EBEDEF"
time.sleep(0.5)
bottom_message['text'] = "就绪"
else:
bottom_message['text'] = "未就绪"
showwarning(title='提示信息', message='服务进程不存在!')
bvar1.set('运行')
bottom_message['text'] = "就绪"
check_btn()
switch_btn['background'] = "#EBEDEF" def run_shell(run_param):
mark = time.strftime('RA+%Y%m%d %H:%M:%S', time.localtime()) # 用于进程名称的特征字符串,方便过滤
cmd = 'start run_assistant.bat "%s" %s' % (mark, run_param)
console = subprocess.Popen(cmd, shell=True)
if run_param == "python manage.py runserver":
return
root.withdraw()
console.wait()
while True:
task_info = subprocess.Popen('tasklist /V | findstr /C:"%s"' % mark, shell=True, stdout=subprocess.PIPE)
if not task_info.stdout.read():
root.deiconify()
break bvar1 = StringVar()
bvar1.set('运行') label1 = Label(root, text='web服务',width=25,borderwidth=2,relief='groove',background='#f60',foreground='white')
switch_btn = Button(root, textvariable=bvar1,background='#EBEDEF',command=state_sw)
label1.grid(row=0,column=0,columnspan=5,padx=15,pady=10,ipadx=5,ipady=6)
switch_btn.grid(row=0,column=5,padx=30,pady=10,ipadx=5,ipady=2) label2 = Label(root, text='管理终端',width=25,borderwidth=2,relief='groove',background='#f60',foreground='white')
button2 = Button(root, text='运行',background='#EBEDEF',command=lambda:run_shell('python manage.py shell'))
label2.grid(row=1,column=0,columnspan=5,padx=15,pady=10,ipadx=5,ipady=6)
button2.grid(row=1,column=5,padx=30,pady=10,ipadx=5,ipady=2) label3 = Label(root, text='数据库终端',width=25,borderwidth=2,relief='groove',background='#f60',foreground='white')
button3 = Button(root, text='运行',background='#EBEDEF',command=lambda:run_shell('python manage.py dbshell'))
label3.grid(row=3,column=0,columnspan=5,padx=15,pady=10,ipadx=5,ipady=6)
button3.grid(row=3,column=5,padx=30,pady=10,ipadx=5,ipady=2) button_index = Button(root, text='首页',command=lambda:open_explo('127.0.0.1:8000/index'))
button_index.grid(row=4,column=3,padx=10,ipadx=5,ipady=2)
button_admin = Button(root, text='控制台',command=lambda:open_explo('127.0.0.1:8000/admin'))
button_admin.grid(row=4,column=4,ipady=2) bottom_message = Label(foreground='blue',width=36,anchor='w',font=('Arial', 8))
bottom_message.grid(row=5,column=0,columnspan=6,padx=15,ipadx=5,sticky='W') ifSetup = find_process()
check_btn()
if ifSetup:
root.withdraw()
if askyesno(title='提示信息', message='8000 端口已被占用,是否帮您停止对应服务?'):
kill_process(ifSetup)
bottom_message['text'] = "就绪"
else:
switch_btn.config(state=tkinter.DISABLED)
bottom_message['text'] = "未就绪"
root.deiconify() if __name__ == '__main__':
root.mainloop()
run_assistant.bat文件:
@echo off
:: %pyenv% 为python虚拟环境根目录
cd "%pyenv%\Scripts"
call activate.bat
:: %project% 为Django项目根目录
cd "%project%"
:: %2 %3 %4 接收的3个参数为要执行的命令行语句
%2 %3 %4
界面截图:

Python-使用tkinter实现的Django服务进程管理工具的更多相关文章
- Python学习(四十三)—— Djago-admin管理工具
一.admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTAL ...
- Django admin 管理工具
admin 组件的使用 Django 提供了基于 web 的管理工具.Django 自动管理工具是 django.contrib 的一部分. INSTALLED_APPS = [ 'django.co ...
- Django——admin管理工具
一.admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTAL ...
- 吴裕雄--天生自然Django框架开发笔记:Django Admin 管理工具
Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 settings.py 中的 INSTALLED_APPS 看到它: ...
- Python开发程序:简单主机批量管理工具
题目:简单主机批量管理工具 需求: 主机分组 登录后显示主机分组,选择分组后查看主机列表 可批量执行命令.发送文件,结果实时返回 主机用户名密码可以不同 流程图: 说明: ### 作者介绍: * au ...
- python之tkinter使用-Grid(网格)布局管理器
# 使用tkinter编写登录窗口 # Grid(网格)布局管理器会将控件放置到一个二维的表格里,主控件被分割为一系列的行和列 # stricky设置对齐方式,参数N/S/W/E分别表示上.下.左.右 ...
- 每日一问:Python生成器和迭代器,with上下文管理工具
1.生成器: 1.1 起源: 如果列表中有一万个元素,我们只想要访问前面几个元素,对其进行相关操作,通过for循环方式效率太低,并且后面的元素会浪费内存,还会受到内存限制,所以产生生成器来解决这个问题 ...
- Django项目实践4 - Django网站管理(后台管理员)
http://blog.csdn.net/pipisorry/article/details/45079751 上篇:Django项目实践3 - Django模型 Introduction 对于某一类 ...
- Django项目实践4 - Django站点管理(后台管理员)
http://blog.csdn.net/pipisorry/article/details/45079751 上篇:Django项目实践3 - Django模型 Introduction 对于某一类 ...
随机推荐
- C# Winform 学习(四)
目标 1.文本类 2.选择类 3.导航类 一.文本类控件 1.文本标签Label 1)命名:lbl开始 2)常用属性: Text.Visible 2.文本框TextBox 1)命名:txt开始 2)常 ...
- Java实现 LeetCode 419 甲板上的战舰
419. 甲板上的战舰 给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直 ...
- Java实现 蓝桥杯VIP 算法训练 统计单词个数
题目描述 给出一个长度不超过200的由小写英文字母组 成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份 (1< k< =40),且每份中 ...
- Java实现 洛谷 Car的旅行路线
输入输出样例 输入样例#1: 1 3 10 1 3 1 1 1 3 3 1 30 2 5 7 4 5 2 1 8 6 8 8 11 6 3 输出样例#1: 47.5 import java.util. ...
- java实现求二十一位水仙花数(21位水仙花数)
一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数. 例如: 当N=3时,153就满足条件,因为 1^3 + 5^3 + 3^3 = 153,这样的数字也被称为水 ...
- CVE¬-2020-¬0796 漏洞复现(本地提权)
CVE-2020-0796 漏洞复现(本地提权) 0X00漏洞简介 Microsoft Windows和Microsoft Windows Server都是美国微软(Microsoft)公司的产品 ...
- zabbix内存百分比监控告警
本文结合配置内存不足10%触发报警的需求,zabbix给我们提供的模板,里面都已经配置好了item和trigger.但是给我们的模板是当内存小于20M的时候才会触发报警,这样不能满足我们的需求,我们需 ...
- KVM虚拟机使用NAT+iptables做端口映射
环境介绍 有一个KVM宿主机,一个外网IP绑定在了宿主服务器上,但是希望直接用ssh访问上面的所有虚拟机,还想虚拟机提供外网服务, 解决方法如下: 环境为RHEL6.3,外网IP为 61.155.xx ...
- Telegraf和Grafana监控多平台上的SQL Server-自定义监控数据收集
问题 在上一篇文章中,我们使用Telegraf自带的Plugin配置好了的监控,但是自带的Plugin并不能完全覆盖我们想要的监控指标,就需要收集额外的自定义的监控数据,实现的方法有: 开发自己的Te ...
- 2个线程A-B-A-B或者B-A-B-A循环输出
代码: /** * 两个线程循环打印输出a-b-a-b */ public class AandBforTOthread { private static Object o = new Object( ...