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服务进程管理工具的更多相关文章

  1. Python学习(四十三)—— Djago-admin管理工具

    一.admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTAL ...

  2. Django admin 管理工具

    admin 组件的使用 Django 提供了基于 web 的管理工具.Django 自动管理工具是 django.contrib 的一部分. INSTALLED_APPS = [ 'django.co ...

  3. Django——admin管理工具

    一.admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTAL ...

  4. 吴裕雄--天生自然Django框架开发笔记:Django Admin 管理工具

    Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 settings.py 中的 INSTALLED_APPS 看到它: ...

  5. Python开发程序:简单主机批量管理工具

    题目:简单主机批量管理工具 需求: 主机分组 登录后显示主机分组,选择分组后查看主机列表 可批量执行命令.发送文件,结果实时返回 主机用户名密码可以不同 流程图: 说明: ### 作者介绍: * au ...

  6. python之tkinter使用-Grid(网格)布局管理器

    # 使用tkinter编写登录窗口 # Grid(网格)布局管理器会将控件放置到一个二维的表格里,主控件被分割为一系列的行和列 # stricky设置对齐方式,参数N/S/W/E分别表示上.下.左.右 ...

  7. 每日一问:Python生成器和迭代器,with上下文管理工具

    1.生成器: 1.1 起源: 如果列表中有一万个元素,我们只想要访问前面几个元素,对其进行相关操作,通过for循环方式效率太低,并且后面的元素会浪费内存,还会受到内存限制,所以产生生成器来解决这个问题 ...

  8. Django项目实践4 - Django网站管理(后台管理员)

    http://blog.csdn.net/pipisorry/article/details/45079751 上篇:Django项目实践3 - Django模型 Introduction 对于某一类 ...

  9. Django项目实践4 - Django站点管理(后台管理员)

    http://blog.csdn.net/pipisorry/article/details/45079751 上篇:Django项目实践3 - Django模型 Introduction 对于某一类 ...

随机推荐

  1. Java实现 蓝桥杯VIP 算法提高 最长字符序列

    算法提高 最长字符序列 时间限制:1.0s 内存限制:256.0MB 最长字符序列 问题描述 设x(i), y(i), z(i)表示单个字符,则X={x(1)x(2)--x(m)},Y={y(1)y( ...

  2. Java实现 洛谷 P1914 小书童——密码

    import java.util.Scanner; public class Main { private static Scanner cin; public static void main(St ...

  3. git提交拉取远程仓库

    https://gitee.com/ ---- 国内服务器 https:/github.com/ ---- 国外服务器 git init  ---- 初始化(创建主分支)仓库 git clone 拉取 ...

  4. KVM Web管理平台 WebVirtMgr

    WebVirtMgr介绍 WebVirtMgr是一个KVM管理平台,让kvm管理变得更为可视化,对中小型kvm应用场景带来了更多方便.WebVirtMgr采用几乎纯Python开发,其前端是基于Pyt ...

  5. [蓝桥杯]算法提高 GPA

    问题描述 输入A,B两人的学分获取情况,输出两人GPA之差. 输入格式 输入的第一行包含一个整数n表示A的课程数,以下n行每行Si,Ci分别表示第i个课程的学分与A的表现. GPA=Σ(Si*Ci) ...

  6. Flask g 对象

    1.什么是g对象? 在 flask 中,有一个专门用来存储用户信息的 g 对象,g的全称的为global. g 对象在一次请求中的所有的代码的地方,都是可以使用的. 赋值方式 from flask i ...

  7. 曹工说JDK源码(4)--抄了一小段ConcurrentHashMap的代码,我解决了部分场景下的Redis缓存雪崩问题

    曹工说JDK源码(1)--ConcurrentHashMap,扩容前大家同在一个哈希桶,为啥扩容后,你去新数组的高位,我只能去低位? 曹工说JDK源码(2)--ConcurrentHashMap的多线 ...

  8. 如何在centos7安装dnf软件包

    想在自己的笔记本CentOS7上安装dnf玩玩儿,但是根据百度出来的方法没有成功. yum install epel-release -y yum install dnf 现在将解决办法转载过来,如下 ...

  9. Docker学习 ,超全文档!

    我们的口号是:再小的帆也能远航,人生不设限!!        一.学习规划: Docker概述 Docker安装 Docker命令 Docker镜像 镜像命令 容器命令 操作命令 容器数据卷  Doc ...

  10. cookie与session区别?

    参考 cookie与session的区别是什么 Java中Cookie的使用(Cookie 和Session的区别) 什么是会话? 用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭 ...