This is a simulation of the FTP upload function, after the client input command, the client executes a command, the input is uploaded to the server in return, in front of a similar practice example
Client
#Small trial Sledgehammer#Import Socket#kt=socket.socket () #创建socket对象#info= ("127.0.0.1", 8800)#Kt.connect (Info)#While True:#Import subprocess#res = subprocess. Popen ("ipconfig", #实例化Popen类 #并且接受的值是byte类型#Shell=true,#stderr=subprocess. PIPE,#stdout=subprocess. PIPE)## Print (Len (Res.stdout.read () decode ("GBK")) # instance res reads data through the Stdout.read () method## print (">>>hello") # This place can't write print#Kt.send (Res.stdout.read ())############################################################################use=input ("User name:")#pwd=input ("Password:")#val= ("%s|%s"% (use,pwd)). Encode ("UTF8") #把多个变量同时传过去############################################################################Importsocketkt=socket.socket ()#creating a Socket objectInfo= ("127.0.0.1", 8800) Kt.connect (info) whileTrue:Importsubprocess cmd=input (">>> Please enter a command") Res= subprocess. Popen (CMD,#instantiate a class of Popen #并且接受的值是byte类型Shell=True, stderr=subprocess. PIPE, stdout=subprocess. PIPE)#print (Len (Res.stdout.read () decode ("GBK")) # instance res reads data through the Stdout.read () method #print (">>>hello") # This place can't write print #Out=res.stdout.read (). Decode ("GBK") #写两个变量不让冲突, all working on RES will have problems #print (len (out)) #Kt.send (Res.stdout.read ()) out=res.stdout.read () Err=Res.stderr.read ()Print("Out length", Len (out))Print("Err Length", Len (err))#Building Baotou Importstruct#header_pack = Struct.pack ("i", Len (out)) #把数据的长度打包成包头, and data sticky packets sent past, on the service side in unpacking #the contents of the Baotou to sendHeader_pack = Struct.pack ("I", Len (out))#I-mode can only package compressed integer class, the result of packaging is 4 bytes, can be sent directly #Send Header,kt.send (header_pack)#Send contentKt.send (out)#the Baotou and the content will be passed together as a sticky bag and untied in the other side. #the time interval is particularly short, and the two rounds will be sent as a package.
Client
Service side
#Small trial Sledgehammer#Import Socket#Sock=socket.socket () #创建socket对象, which is followed by operations on this object##info= ("127.0.0.1", 8800)#Sock.bind (Info)#Sock.listen (5)#While True:#conn,addr=sock.accept () #开始阻塞## Print ("received successfully!")#Data=conn.recv (1024x768) #recv的参数是固定长度#Print (Data.decode ("GBK"))#Print ("Receive successfully!")############################################################################################who's conn? ImportSocketImportStructsock=socket.socket ()#creates the socket object, which is followed by operations on the objectInfo=("127.0.0.1", 8800) Sock.bind (info) sock.listen (5) whiletrue:conn,addr=sock.accept ()#Start blocking #Print ("Receive successfully!")DATA=CONN.RECV (4)#the parameters of the recv are fixed lengthsData_length=struct.unpack ("I", data) [0]#The Other side is 4 bytes long, after unpacking is the length of the number, default in the tuple, with the index out Print(data)Print(data_length)Print("Receive success!") Recv_data_length=0 Recv_data=b"" whileRecv_data_length<data_length:#The length of the data_length is transmitted by the other side, this is the largestDATA=CONN.RECV (1024) Recv_data_length+=len (data) Recv_data+=DataPrint(">>>>>>>%s"%type (recv_data))Print(Recv_data.decode ("GBK")) #Data_length=int (CONN.RECV (1024x768) decode ("UTF8"))
Service Side
Some of the knowledge points used are:
struct MODULE
Subprocess Module
Network Programming--ftp Upload