Development background:
Prior to an internship in an IDC company, responsible for server aftermarket, often use ping,tracert,tcping and other commands to do some preliminary diagnosis, to determine the server problem in what aspect. So I want to integrate these common commands or tools into a GUI interface, implement one-click Call. (PS: Of course the company has better integration tools, I just imitate the company's)
Development environment:
Win 7 64-bit, Python 3.6.2,wxpython
Specific ideas:
Through the Wxpython implementation GUI interface, through the subprocess. Popen creates a new process to execute the call of the DOS command implementation tool.
Feature implementation:
GUI Interface:
To implement the ping,tracert,tcping command:
Key steps:
Button Event code:
self.Bind(wx.EVT_BUTTON,lambda _:subprocess.Popen(‘cmd /k ping -t ‘+tc.GetValue().split(‘:‘)[0]),b1)wx.EVTBUTTON是按钮事件lambda _:subprocess.Popen(‘cmd /k ping -t ‘+tc.GetValue().split(‘:‘)[0]) 是绑定的函数b1 是ping按钮的变量名
This time the key is in the function of the button event binding.
1. Because the function wants to pass in the DOS command as a parameter, the Lambda anonymous function needs to be used, otherwise the function executes when the program starts, not when the button event occurs.
2. What method to use to invoke DOS command, Os.system (), Os.popen () or subprocess. Popen ().
Os.system () and Os.popen () block the parent process, which means that the GUI interface is not operational until the command is completed. Of course, you can use the Os.system (' Start DOS command ') to solve this problem, the DOS Start command is to start a separate window to run the specified program or command. Because the new window is started quickly, so it is not feel blocked, but there is a new problem, so there will be two DOS window, execute Start window and Start window, execute Start Command window will flash back (immediately after execution of the exit)
So in the end I chose subprocess. Popen (), because subprocess. Popen () Creates a new process without blocking the parent process.
3, the choice of DOS command
cmd /k ping -t ‘+tc.GetValue().split(‘:‘)[0]
Cmd/k is the execution of the command still preserves the cmd window
Tc. GetValue () is the IP address of the input box, because sometimes the format of the input box is ip:port, so the TC is used. GetValue (). Split (': ') [0] to get the IP
Specific source code:
# coding:utf-8import Wx#import osimport subprocessclass myframe (WX. Frame): Def __init__ (self, Parent, title): #设置窗口标题, Window size, window cannot change size WX. Frame.__init__ (self, parent, Title=title, size= ( -1, 530), pos= (100,100), style=wx. Default_frame_style ^ Wx. Resize_border ^ Wx. Maximize_box) self. Initui () #self. Centre () #窗口居中 self. Show () def initui (self): #显示窗口组件内容 panel=wx. Panel (self) font = WX. Font (WX. SWISS, WX. NORMAL, WX. NORMAL) #创建输入框 text=wx. Statictext (panel, label= ' IP: ') text. SetFont (font) tc=wx. Textctrl (panel,size= (200,-1), Style=wx.te_center) TC. SetFont (font) #创建按钮, and binds event b1=wx. Button (panel, label= ' ping ') B1. SetFont (font) self. Bind (WX. Evt_button,lambda _:subprocess. Popen (' cmd/k ping-t ' +TC. GetValue (). Split (': ') [0]), B1) b2=wx. Button (panel, label= ' tracert ') B2. SetFont (font) self. Bind (WX. Evt_button,lambda _:subprocess. Popen (' cmd/k tracert-d ' +TC. GetValue (). SPLIt (': ') [0]), B2) b3=wx. Button (panel, label= ' tcping ') B3. SetFont (font) self. Bind (WX. Evt_button,lambda _:subprocess. Popen (' cmd/k tools\\tcping-t ' +TC. GetValue (). Replace (': ', '), B3) b4=wx. Button (panel, label= ' mstsc ') b4. SetFont (font) self. Bind (WX. Evt_button,lambda _:subprocess. Popen (' mstsc/v ' +TC. GetValue ()), B4) b5=wx. Button (panel, label= ' Putty ') B5. SetFont (font) self. Bind (WX. Evt_button,lambda _:subprocess. Popen (' Tools\\putty ' +TC. GetValue () + '-P ' + (TC. GetValue () + ': '). Split (': ') [1]), B5) b6=wx. Button (panel, label= ' multiple Ping-pinginfoview ') B6. SetFont (font) self. Bind (WX. Evt_button,lambda _:subprocess. Popen (' Tools\\pinginfoview '), b6) font1 = wx. Font (one, WX. SWISS, WX. NORMAL, WX. NORMAL) #操作说明 t1=wx. Statictext (Panel, label= ' Description: ') t1. SetFont (WX. Font (WX. SWISS, WX. NORMAL, WX. NORMAL)) t2=wx. Statictext (panel, label= ' input format: ip:port\n If only input IP, the port is 3389\n if the input is empty, then directly open mstsc ') t2. SetFont (font1) t3=wx. Statictext (panel, label= ' mstsc description: ') T3. SetFont (font1) t4=wx. Statictext (panel, label= ' Putty Description: ') T4. SetFont (font1) t5=wx. Statictext (panel, label= ' input format: ip:port\n If only input IP, the port is 22\n if the input is empty, then directly open Putty ') T5. SetFont (font1) t7=wx. Statictext (panel, label= ' tcping Description: ') T7. SetFont (font1) t8=wx. Statictext (panel, label= ' input format: ip:port\n If only input IP, port is ") T8. SetFont (font1) #使用FlexGridSizer布局 fgs=wx. Flexgridsizer (cols=2,hgap=10,vgap=10) FGs. Addmany ((TEXT,0,WX), ((), (). Align_right | Wx. align_center_vertical), (tc,0,wx. EXPAND), (B1,0,WX), (a). EXPAND), (B2,0,WX), (a). EXPAND), (B3,0,WX), (a). EXPAND), (B4,0,WX), (a). EXPAND), (B5,0,WX), (a). EXPAND), (B6,0,WX), (a). EXPAND), (T1), (+), (T7), (T8), (T3), (T2), (T4), (T5)]) panel. Setsizerandfit (FGS) if __name__== ' __main__ ': app=wx. App () myframe (none,title= ' tools) ' app. Mainloop ()
Source code and tools have been uploaded in http://down.51cto.com/data/2447030
Thinking Summary
This time the code is more than 60 lines, the implementation of the integration tool, feel that the use of Python development is very convenient.
Python Integrated Network Diagnostics gadget (contains ping,tracert,tcping and other gadgets)