A: Socket basic establishment
#server端
#Encoding:utf-8"""@version: 3.6.6@author:duke@file:server.py@time:2018/5/6/006 0:03"""#The Socket Programming application layer and the Transport layerImportSocketsk=socket.socket () address= ("127.0.0.1", 8000) Sk.bind (address) Sk.listen (3)#parameter set the number of connections can be connected, the number of blockedCONN,ADDR =sk.accept () InP= Input ("Input transfer Data >>>") conn.send (bytes (INP,'Utf-8')) Conn.close () sk.close ()
#Client Side#Encoding:utf-8"""@version: 3.6.6@author:duke@file:client.py@time:2018/5/6/006 21:24"""ImportSocketsk=Socket.socket ()#print (SK)Address = ('127.0.0.1', 8000) Sk.connect (address) data= SK.RECV (1024)Print(Str (data,'Utf-8') ) Sk.close ()
II: Remote execution of socket application commands
#Ssh_servere End#Encoding:utf-8"""@version: 3.6.6@author:duke@file:ssh_server.py@time:2018/5/9/009 10:18"""ImportSocketImportSubprocesssk=socket.socket () address= ('127.0.0.1', 8000) Sk.bind (address) Sk.listen (3) whiletrue:conn,addr=sk.accept () whileTrue:Try: Data= CONN.RECV (1024) exceptException: Break if notData Break Print('.......', str (data,'Utf-8')) obj= subprocess. Popen (str (data,'Utf-8'), Shell = true,stdout=subprocess. PIPE)#encapsulated as a pipe to the main process, encapsulated as an object, saved in an objectSsh_result = Obj.stdout.read ()#data is bytes #print (Ssh_result, ' GBK ')Conn.send (bytes (str (ssh_result)),'Utf-8')) Conn.recv (1024) conn.send (Ssh_result) conn.close () sk.close ()
#ssh_client端#Encoding:utf-8"""@version: 3.6.6@author:duke@file:ssh_client.py@time:2018/5/9/009 10:26"""ImportSocketsk=Socket.socket ()#print (SK)Address = ('127.0.0.1', 8000) Sk.connect (address) whileTrue:send_data= Input ("Please enter the command to execute >>>") ifSend_data = ='Exit': Breaksk.send (Bytes (Send_data,'Utf-8')) Data_len= STR (SK.RECV (1024),'Utf-8') sk.send (bytes ("solving Sticky pack problems",'Utf-8')) Data_len=Int (data_len)Print(data_len) data=bytes () whileData_len:recv_data= SK.RECV (1024) Data+=Recv_data Data_len-=Len (recv_data)Print(Str (data,'GBK') ) Sk.close ()
Three: File Upload
#Server Side#Encoding:utf-8"""@version: 3.6.6@author:duke@file:file_server.py@time:2018/5/11/011 10:02"""ImportSocketImportOSImportSubprocesssk=socket.socket () address= ('127.0.0.1', 8000) Sk.bind (address) Sk.listen (3) Base_dir= Os.path.dirname (Os.path.abspath (__file__)) whiletrue:conn,addr=sk.accept () whileTrue:Try: Data= CONN.RECV (1024) Print(data) cmd,file_name,file_size= str (data,'Utf-8'). Split ('|') path= Os.path.join (Base_dir,'Yuan', file_name)#Conn.sendall ("111") Print(cmd,file_name,file_size)Print(PATH)exceptException: Break if notData Breakfile_size=Int (file_size) F= Open (Path,"WB") whileFile_size:write_data= CONN.RECV (1024) Print(Write_data) f.write (write_data) file_size= File_size-Len (write_data) F.close () Conn.close () sk.close ()
#client端
#Encoding:utf-8"""@version: 3.6.6@author:duke@file:file_client.py@time:2018/5/11/011 10:03"""ImportSocketImportOssk=Socket.socket ()#print (SK)Address = ('127.0.0.1', 8000) Sk.connect (address) Base_dir= Os.path.dirname (Os.path.abspath (__file__)) whileTrue:send_data= Input ("Please enter the command to execute >>>")#Post + path ifSend_data = ='Exit': BreakCmd,path= Send_data.split ('|') path=Os.path.join (Base_dir,path)Print(path) filename=os.path.basename (path) file_size=os.stat (path). St_size File_info="%s|%s|%s"%(cmd,filename,file_size)Print(file_info) sk.sendall (bytes (File_info,'Utf-8')) #Sk.recv (1024x768)File_size =Int (file_size) F= Open (Path,'RB') whileFile_size:data= F.read (1024) Sk.sendall (data) file_size= File_size-len (data) F.close () sk.close ()
Python Learning Tcpsocket