socket large file transfers (to solve the stick package)

Solution stick package 
module struct 
      struct.pack (type, num) 
          type: the type is num 
          num: is a number 
          R & lt = struct.pack to a digital packed into a four-byte bytes 
      struct.unpack (type, R & lt) 
          function: unpacking, the r dissociate into the original numbers, the result is a tuple, the original numbers in subscript bit positions tuple 0 
# solution stick package: the principle is that the server receives the dictionary length, according to the dictionary length to recv content dictionary it will not cause recv after receiving the last remaining space for the contents of the file content dictionary and file contents caused by sticking together 
          
# implementation file upload feature   
# Client layer 
Import socket
 Import json
 Import struct
 Import os 

SK = socket .socket () 
sk.connect (( " 10.70.2.143 " , 8080 )) 
the Option={"1":"upload","2":"download"}
for index,value in option.items():
    print(index,value)
num=input("请输入您的选择")
if num=="1":
    dic={"opt":option.get(num),"filename":None,"filesize":None}
    file_path=input(" Please enter the file upload required absolute path " ) 
    filename = os.path.basename (file_path) 
    filesize = os.path.getsize (file_path) 
    dic [ " filename " ] = filename 
    dic [ " filesize " ] = filesize
     # the dictionary it to a string 
    str_dic = json.dumps (DIC)
     # Get the length of the dictionary, in order to solve the stick package 
    len_dic = len (str_dic)
     # solve the stick package: the principle is that the server receives the length of the dictionary, the dictionary according to the length of recv contents of the dictionary, it will not cause recv after receiving the last remaining space for the contents of the file content dictionary and file contents caused by sticking together 
    # in the dictionary data length of a 4bytes 
    # when the server receives first receiving recv (4) bytes in length will be the length of the dictionary obtained
    = struct.pack b_len_dic ( ' I ' , len_dic)   # I is the data type of the original length of the dictionary, the second parameter is a dictionary that need to be converted to the length itself 
    # The bytes length and type of dictionaries and dictionary content splicing transmission 
    sk. Send (b_len_dic + str_dic.encode ( " UTF-. 8 " ))   # At this time the contents of the dictionary are bytes type splicing can be performed bytes 

    # uploaded file 
    with Open (file_path, " RB " ) AS F:
         # according to the file size Upload file 
        the while filesize: 
            Content = f.read (1024 ) 
            sk.send (Content) 
            filesize - = len (Content)
the else :
     Pass 
sk.close () 






# Server layer 
Import Socket
 Import JSON
 Import struct 


SK = socket.socket () 
sk.bind (( " 10.70.2.143 " , 8080 )) 
sk.listen () 
Conn, addr = sk.accept ()
 # first received from a client dictionary pass over the length of four bytes in length 
b_len_dic conn.recv = (. 4 )
 # the length of the byte length of the dictionary is a dictionary unpacks integer length 
len_dic = struct.unpack ( ' I ' , b_len_dic) [0] # because the dictionary is passed over the length of a tuple of elements subscript 0 
# to receive the contents of the dictionary in accordance with the length of the dictionary 
#The next time the file transfer will not cause stick pack phenomenon
= conn.recv str_dic (len_dic) .decode ( " UTF-. 8 " )
 # The deserialized into the dictionary string dictionary 
DIC = json.loads (str_dic)
 IF DIC [ " opt " ] == " Upload " :
     # for to prevent the same file name, file name string splicing distinguished 
    filename = " . 1 " + DIC [ " filename " ]
     # received from the client over the transfer file 
    with Open (filename, " ab & " ) AS F:
         # according to the received file size data 
        the while dic [ " filesize]:"
            Content = conn.recv (1024 ) 
            f.write (Content) 
            # dictionary file size minus the accepted file size 
            dic [ " filesize " ] - = len (Content)
 elif dic [ " opt " ] == " download " :
     Pass 
conn.Close () 
sk.close ()

 

Guess you like

Origin www.cnblogs.com/god-for-speed/p/11719047.html