好久没有更新博客了,最近一直在学习python,于是,在有空闲的时间写下python视图界面管理nginx,写得不够好,不过希望能帮忙到其他童鞋,再次感叹,强大的python,不说了,上代码。

 
  
  1. [root@localhost ~]# cat /opt/python.py  
  2. #-*- encoding=UTF-8 -*- 
  3. import Tkinter 
  4. from Tkinter import * 
  5. from FileDialog import * 
  6. import os 
  7. root = Tk() 
  8. root.title('nginx管理GUI') 
  9. root.geometry('800x400') 
  10. t = Tkinter.Text(root,height=20,width=100,bg='grey',wrap = 'word' ) 
  11.  
  12. def openfile(): 
  13.     t.delete(1.0, 'end') 
  14.     fd = LoadFileDialog(root) 
  15.     filename = fd.go() 
  16.     content = open(filename, 'r') 
  17.     linescontent.readlines() 
  18.     for line in lines: 
  19.         t.insert('end',line) 
  20. #    file.close() 
  21.  
  22. def savefile(): 
  23.     fd = SaveFileDialog(root) 
  24.     filenamefd.go() 
  25.     file = open(filename, 'w') 
  26.     content = t.get(1.0, END) 
  27.     file.write(content) 
  28.     file.close() 
  29.  
  30.  
  31. def threads(): 
  32.     t.delete(1.0, 'end') 
  33.     result = os.popen('ps -ef | grep httpd | grep -v grep|wc -l').readlines() 
  34.     t.insert(INSERT, "\n".join(result), "a") 
  35.  
  36.  
  37. def status(): 
  38.     t.delete(1.0, 'end') 
  39.     result = os.popen('sh status.sh').readlines() 
  40.     t.insert(INSERT, "\n".join(result), "a") 
  41.  
  42. def total(): 
  43.     t.delete(1.0, 'end') 
  44.     result = os.popen('sh conn.sh').readlines() 
  45.     t.insert(INSERT, "\n".join(result), "a") 
  46.  
  47.  
  48.  
  49.  
  50.  
  51. def start(): 
  52.     t.delete(1.0, 'end') 
  53.     result = os.popen('service httpd start').readlines() 
  54.     t.insert(INSERT, "\n".join(result), "a") 
  55. def stop(): 
  56.     t.delete(1.0, 'end') 
  57.     result = os.popen('service httpd stop').readlines()     
  58.     t.insert(INSERT, "\n".join(result), "a") 
  59. def restart(): 
  60.     t.delete(1.0, 'end') 
  61.     result = os.popen('service httpd restart').readlines() 
  62.     t.insert(INSERT, "\n".join(result), "a") 
  63.  
  64.  
  65.  
  66.  
  67. def about(): 
  68.     t.delete(1.0, 'end') 
  69.     w = Label(root,text="这个是nginx管理的第一个版本,谢谢大家支持"
  70.     w.pack(side=TOP
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. menubar = Menu(root) 
  78. #创建下拉菜单File,然后将其加入到顶级的菜单栏中 
  79. filemenu = Menu(menubar,tearoff=0
  80. filemenu.add_command(label="打开配置文件"command=openfile
  81. filemenu.add_command(label="保存配置文件"command=savefile
  82. filemenu.add_separator() 
  83. filemenu.add_command(label="退出"command=root.quit) 
  84. menubar.add_cascade(label="nginx配置管理"menu=filemenu
  85.  
  86. #创建一个下拉菜单Edit 
  87. editmenu = Menu(menubar, tearoff=0
  88. editmenu.add_command(label="总线程数"command=threads
  89. editmenu.add_command(label="状态"command=status
  90. editmenu.add_command(label="连接数"command=total
  91. menubar.add_cascade(label="nginx基本监控查看",menu=editmenu
  92.  
  93. #创建下拉菜单status 
  94. editmenu = Menu(menubar, tearoff=0
  95. editmenu.add_command(label="启动nginx"command=start
  96. editmenu.add_command(label="停止nginx",command=stop
  97. editmenu.add_command(label="重启nginx"command=restart
  98. menubar.add_cascade(label="nginx操作",menu=editmenu
  99. #创建下拉菜单Help 
  100. helpmenu = Menu(menubar, tearoff=0
  101. helpmenu.add_command(label="about"command=about
  102. menubar.add_cascade(label="查看版本和帮助"menu=helpmenu
  103. #显示菜单 
  104. root.config(menu=menubar
  105.  
  106. #显示菜单 
  107. t.pack() 
  108. mainloop() 

注意,这里需要的模块,还有要在图形界面的系统

执行结果:

 

下面的是其他功能查看

 

 

 

 

写得不是很完美,希望拍砖