"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> Ps_client.py>> Author: Liu Yang>> e-mail: [email protected] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "." "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" #!/usr/bin/env python#-*-coding:utf-8-*-import sys, osfrom, "" ". Socket import *from tkinter import *class ps_client (): Def __init__ (self): Self.ip=none Self.port=none Self.data=none # Create IPv4 socket Self.client=socket (af_inet,sock_stream) self.root=tk () self.ro Ot.geometry (' 300x300+250+250 ') # Create IP input Box VAR_IP = Stringvar () var_ip.set (' localhost ') self.et_ Ip=entry (SELF.ROOT,WIDTH=30,TEXTVARIABLE=VAR_IP) Self.et_ip.pack () # Create the label Self.ip_lable=labe of the IP input box L (self.root,text= "IP Address") # Create port number input Box Var_port = Stringvar () var_port.set (self.et_port=entry) (Self.root,width=30,textvariable=var_port) # Create port number Label Self.port_lable=label (self.root,text= "Port number") # Create connection button, NOTE!!! command= Self.connbutten=button (self.root,text= "Connect", Command=self.connect) # Creates a Get button on the back of the connection Self.get Butten=button (self.root,text= "Get", state= ' disable ', command=self.get_cpu_info) # Create Disconnect button Self.exitbutten=button (self.root,text= "Exit", state= ' Disable ') Self.txtbox=text (self.root,width=40,height=10) def main (self): self . Et_ip.place (x=5,y=20) self.et_port.place (x=5,y=50) self.ip_lable.place (x=230,y=20) SELF.PORT_LABLE.P Lace (x=230,y=50) self.connButten.place (x=5,y=80) self.getButten.place (x=90,y=80) Self.exitButten.plac E (x=180,y=80) self.txtBox.place (x=5,y=120) self.txtBox.insert (INSERT, ' Show content here ') Self.root.mainloop () def connect (self): Self.ip=self.et_ip.get () Try:self.port=int (Self.et_port.get ()) Exce PT ValueError:self.txtBox.delete (0.0,end) Self.txtBox.insert (0.0, "Please enter a valid IP and port ...") Else:print ("ip:%s"%self.ip) print ("port:%s"%self . Port) Self.txtBox.delete (0.0,end) self.txtBox.insert (0.0, "in link ...") Try: Self.client.connect ((self.ip,self.port)) except Oserror:print ("a socket operation was attempted to a network that cannot be connected") Self.txtBox.delete (0.0, END) self.txtBox.insert (0.0, "%s:%d connection failed ..."% (Self.ip,self.port)) Else:print ("%s connection succeeded ..."%self.ip) self.txtBox.delete (0.0, END) self.txtBox.ins ERT (0.0, "%s:%d connected successfully ..."% (Self.ip,self.port)) # The connection is successful and the other button is changed to a status of Self.exitbutten[' state ']= ' AC tive ' self.getbutten[' state ']= ' active ' def get_cpu_info (self): self.data= ' CPU ' self.client . Send (Self.data.encode (' Utf-8 ')) Cpu_used=self.client.recv (1024x768). Decode (' Utf-8 ') print (cpu_used) self . Txtbox.delete (0.0, END) # string preceded by R for anti-escape Self.txtBox.insert (0.0, "Current CPU usage:%s"%cpu_used+r "%") def exit_connect (self): self. Client.close () self.txtBox.delete (0.0, END) self.txtBox.insert (0.0, "Current connection disconnected ...") if __name__ = = ' __main__ ': Ps=ps_client () Ps.main ()
[Python Study notes]cs schema remote access get information--client end