Because this is simple, so it's easy.
The idea is that after the connection is established, the client sends the file name and size to the server, and after the server accepts it, the client begins to send the file until it finishes sending. Specific or to look at the following code, I realized it all over again
1 ImportSocket2 ImportOS3 4 defget_server_socket ():5SK =Socket.socket ()6Address = ('127.0.0.1', 8888)7 Sk.bind (address)8Sk.listen (3)9 returnSKTen One defWait_connect (SK): A Print('waitconnecting ...') -conn, addr =sk.accept () - returnConn the - if __name__=='__main__': -SK =Get_server_socket () -conn =Wait_connect (SK) +Basic_dir = Os.path.dirname (Os.path.abspath (__file__)) - whileTrue: +data = str (CONN.RECV (1024),'UTF8') ACmd,filename,file_size = Data.split ('|') atPath = Os.path.join (Basic_dir,'Yuan', filename) -File_size =Int (file_size) -f = open (path,'WB') -Has_receive =0 - whileHas_receive! =file_size: -data = CONN.RECV (1024) in f.write (data) -Has_receive + =len (data) toF.close ()post_server.py
1 ImportSocket2 ImportOS3 4 defget_client_socket ():5SK =Socket.socket ()6Address = ('127.0.0.1', 8888)7 Sk.connect (address)8 returnSK9 Ten if __name__=='__main__': OneBasic_dir = Os.path.dirname (Os.path.abspath (__file__)) ASK =Get_client_socket () - whileTrue: -INP = input ('>>>'). Strip ()#Post|11.png theCmd,path = Inp.split ('|') -Path = Os.path.join (Basic_dir,path)#using the system's path stitching -filename = os.path.basename (path)#get the file name . -File_size = Os.stat (filename). st_size#get the file size +File_info ='post|%s|%s'%(filename,file_size) -Sk.sendall (Bytes (File_info,'UTF8')) +f = open (path,'RB') AHas_sent =0 at whileHas_sent! =file_size: -data = F.read (1024) - sk.sendall (data) -Has_sent + =len (data) - f.close () - Print('Upload Complete')post_client.py
034 File Upload