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 对于某一类 ...
随机推荐
- Java实现 LeetCode 514 自由之路
514. 自由之路 视频游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并使用表盘拼写 ...
- Java实现 蓝桥杯VIP 算法训练 学做菜
算法训练 学做菜 时间限制:1.0s 内存限制:256.0MB 问题描述 涛涛立志要做新好青年,他最近在学做菜.由于技术还很生疏,他只会用鸡蛋,西红柿,鸡丁,辣酱这四种原料来做菜,我们给这四种原料标上 ...
- Java实现 计蒜客 拯救行动
拯救行动 公主被恶人抓走,被关押在牢房的某个地方.牢房用 N \times M (N, M \le 200)N×M(N,M≤200) 的矩阵来表示.矩阵中的每项可以代表道路(@).墙壁(#).和守卫( ...
- Java实现 LeetCode 45 跳跃游戏 II(二)
45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- Cypress系列(14)- 环境变量详解
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 环境变量,其实就是根据环境的变化, ...
- 数据结构与算法-python描述-单链表
# coding:utf-8 # 单链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历整个链表 # add(item) 链表头部添加元 ...
- 如何设置body内容不能复制?
通过在body标签上设置相关的属性: <body oncontextmenu="return false" ondragstart="return false&qu ...
- 如何在Centos7安装rabbitmq的PHP扩展
1.先安装rabbitmq-c, wget https://github.com/alanxz/rabbitmq-c/releases/download/v0.8.0/rabbitmq-c-0.8.0 ...
- Source Insight 中的 Auto Indenting
编码过程中,希望输入花括号时能自动对齐,Source Insigth 应如何设置? 先来看一下Source Insight 中的帮助. “ Auto Indenting The auto-indent ...
- Windows10 下安装和配置Redis
原文链接:https://blog.csdn.net/linghugoolge/article/details/86608897 一.下载地址https://github.com/MicrosoftA ...