The socket recommended Maximum transmission unit is 8,192 characters, but if more than 8192 will cause problems, we can use the following methods to deal with
Client code
Import Subprocessimport Socketip_bind = ("127.0.0.1", 9000) client = Socket.socket () client.connect (ip_bind) L1 = []while True: option = input ("client:") Client.sendall (bytes (option,encoding= "Utf-8")) server_data_size = Client.recv Print (server_data_size) a = str (server_data_size,encoding= "Utf-8"). Strip () print (a) L1 = A.split (":") print (L1) if l1[0] = = "Server_data_size": client_size_tmp = l1[1] print ( CLIENT_SIZE_TMP) client_size = Int (client_size_tmp) print (Type (client_size)) client.send (Bytes (" Ok ", encoding=" Utf-8 ")) receive_size = 0 res =" "While receive_size < client_size: server_data = CLIENT.RECV (+) receive_size + = Len (server_data) res + = str (server_data) else: print (RES) print ("-------reveive down---------")
Service-side code
Import Subprocessimport Socketip_bind = ("127.0.0.1", 9000) Server = Socket.socket () server.bind (ip_bind) Server.listen ( 1) Print ("Server is Waiting ...") Conn,add = Server.accept () print ("Server is connected client") while True: Client_data = Conn.recv ( a = subprocess). Popen (str (client_data,encoding= "Utf-8"), stdout=subprocess. PIPE) B = A.stdout.read () server_data_size = "server_data_size:" + str (len (b)) Conn.sendall (bytes (server _data_size,encoding= "Utf-8")) Client_ack = Conn.recv (Ten) if str (client_ack,encoding= "utf-8") = = "OK": Conn.sendall (b)
Python socket used to transfer large files