好久没有更新博客了,最近一直在学习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. def openfile():
  12. t.delete(1.0, 'end')
  13. fd = LoadFileDialog(root)
  14. filename = fd.go()
  15. content = open(filename, 'r')
  16. linescontent.readlines()
  17. for line in lines:
  18. t.insert('end',line)
  19. #    file.close()
  20. def savefile():
  21. fd = SaveFileDialog(root)
  22. filenamefd.go()
  23. file = open(filename, 'w')
  24. content = t.get(1.0, END)
  25. file.write(content)
  26. file.close()
  27. def threads():
  28. t.delete(1.0, 'end')
  29. result = os.popen('ps -ef | grep httpd | grep -v grep|wc -l').readlines()
  30. t.insert(INSERT, "\n".join(result), "a")
  31. def status():
  32. t.delete(1.0, 'end')
  33. result = os.popen('sh status.sh').readlines()
  34. t.insert(INSERT, "\n".join(result), "a")
  35. def total():
  36. t.delete(1.0, 'end')
  37. result = os.popen('sh conn.sh').readlines()
  38. t.insert(INSERT, "\n".join(result), "a")
  39. def start():
  40. t.delete(1.0, 'end')
  41. result = os.popen('service httpd start').readlines()
  42. t.insert(INSERT, "\n".join(result), "a")
  43. def stop():
  44. t.delete(1.0, 'end')
  45. result = os.popen('service httpd stop').readlines()
  46. t.insert(INSERT, "\n".join(result), "a")
  47. def restart():
  48. t.delete(1.0, 'end')
  49. result = os.popen('service httpd restart').readlines()
  50. t.insert(INSERT, "\n".join(result), "a")
  51. def about():
  52. t.delete(1.0, 'end')
  53. w = Label(root,text="这个是nginx管理的第一个版本,谢谢大家支持")
  54. w.pack(side=TOP)
  55. menubar = Menu(root)
  56. #创建下拉菜单File,然后将其加入到顶级的菜单栏中
  57. filemenu = Menu(menubar,tearoff=0)
  58. filemenu.add_command(label="打开配置文件"command=openfile)
  59. filemenu.add_command(label="保存配置文件"command=savefile)
  60. filemenu.add_separator()
  61. filemenu.add_command(label="退出"command=root.quit)
  62. menubar.add_cascade(label="nginx配置管理"menu=filemenu)
  63. #创建一个下拉菜单Edit
  64. editmenu = Menu(menubar, tearoff=0)
  65. editmenu.add_command(label="总线程数"command=threads)
  66. editmenu.add_command(label="状态"command=status)
  67. editmenu.add_command(label="连接数"command=total)
  68. menubar.add_cascade(label="nginx基本监控查看",menu=editmenu)
  69. #创建下拉菜单status
  70. editmenu = Menu(menubar, tearoff=0)
  71. editmenu.add_command(label="启动nginx"command=start)
  72. editmenu.add_command(label="停止nginx",command=stop)
  73. editmenu.add_command(label="重启nginx"command=restart)
  74. menubar.add_cascade(label="nginx操作",menu=editmenu)
  75. #创建下拉菜单Help
  76. helpmenu = Menu(menubar, tearoff=0)
  77. helpmenu.add_command(label="about"command=about)
  78. menubar.add_cascade(label="查看版本和帮助"menu=helpmenu)
  79. #显示菜单
  80. root.config(menu=menubar)
  81. #显示菜单
  82. t.pack()
  83. mainloop()

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

执行结果:

下面的是其他功能查看

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