Network Programming uploads

Client 

Import socket
 Import os
 Import json
 Import struct 


Client = socket.socket () 
the client.connect (( " 127.0.0.1 " , 12000 ))
 "" " 
get file path to 
get the file under the file path 
for circulating print plus the enumerated contents of the print file 
the user selects to upload content 
determines whether the digital input 
is then input into the number of type int 
determines whether this number is greater than the content of the document 
taken out by splicing the path selected by the user 
remove the user selects a file's contents 
to create a dictionary , choosing a name out into the contents of 
the serialized dictionary dictionary will become a binary string behind the turn to 
create a dictionary of a header 
sent dictionary header 
dictionary, then send sequence after the 
open file handle for circulating documents, according to 
the transmission cycle after the character 
"" " 

the while True:
    HUOQUR = " D: \ old boy \ learning materials \ videos \ day30 \ video " 
    the PATH   = os.listdir (HUOQU)
     for i, J in the enumerate (the PATH, 1 ):
         Print (i, J) 
    Choice = the INPUT ( " Please input number >>>: " )
     IF choice.isdigit (): 
         Choice = int (Choice) -1
          IF Choice in Range (0, len (the PATH)): 
             Wenjian = the os.path.join (HUOQU, the PATH [Choice] ) 
             neirong = os.path.getsize (Wenjian) 
             D = { " name":"欧美精选",
                 "file_size":neirong}
             byths = json.dumps(d).encode("utf-8")
             suoxiaoshuju = struct.pack("i",len(byths))
             client.send(suoxiaoshuju)
             client.send(byths)

             with open (wenjian,"rb") as f:
                 for i in f:
                     client.send(i)


         else:
             Print (" outside the scope " )
     the else :
         Print ( " not a number " )
Server
 Import socket
 Import json
 Import struct 
Server = socket.socket () 
server.bind (( " 127.0.0.1 " , 12000 )) 
server.listen ( 5 )
 "" " Get file header 
file header untie 
get file 
file deserialization, the string dictionary program 
fetches data in the dictionary 
to create the initial length of the file 
to read data dictionary file 
if data in the file is greater than the determination dictionary data within 
each byte taken 1024 
F. remove the 
file length + each fetched byte "" " 

the while True: 
    Conn, MSG = server.accept ()
     the while True: 

        the try : 
            RES= conn.recv(4)
            res1 = struct.unpack("i",res)[0]
            res2 = conn.recv(res1)
            res3  =json.loads(res2.decode("utf-8"))
            res4 = res3.get("file_size")
            dic_size = 0
            with open(res3.get("name"),"wb") as f:
                while dic_size<res4:
                    data = conn.recv(1024)
                    f.write(data)
                    dic_size+=len(data)
                print("sahngchuan")

        except ConnectionError as e:
            break
    conn.close()

 

Guess you like

Origin www.cnblogs.com/yangxinpython/p/11323422.html