The socket determines the size of the received file and the socket File Based on the file size.

Source: Internet
Author: User

The socket determines the size of the received file and the socket File Based on the file size.

# Server simultaneously performs md5 Encryption
1 import socket 2 import hashlib 3 4 client = socket. socket () 5 client. connect ('localhost', 9998) 6 while True: 7 cmd = input ("Please input :"). strip () 8 if len (cmd) = 0: continue 9 if cmd. startswith ('get'): 10 client. send (cmd. encode () 11 server_response = client. recv (1024) 12 print ("server response:", server_response) 13 client. send (B 'ready to recv file') 14 file_total_size = int (server_response.decode () 15 bytes ed_size = 016 filename = cmd. split () [1] 17 f = open (filename + 'new', 'wb ') 18 m = hashlib. md5 () 19 20 while received_size <file_total_size: 21 if file_total_size-received_size> 1024: # receive more than once 22 size = 102423 else: # The last receive, how much is left to receive, 24 size = file_total_size-received_size25 print ('Last receive: ', size) 26 27 data = client. recv (size) 28 export ed_size + = len (data) 29 m. update (data) 30 f. write (data) 31 else: 32 new_file_md5 = m. hexdigest () 33 print ('file receive done', received_size, file_total_size) 34 f. close () 35 server_file_md5 = client. recv (1024) 36 print ('server file md5: ', server_file_md5) 37 print ('client file md5', new_file_md5) 38 39 client. close ()

 





#client
1 import socket 2 client = socket. socket () 3 client. connect ('localhost', 9999) 4 while True: 5 cmd = input ('enter :'). strip () 6 if len (cmd) = 0: continue 7 client. send (cmd. encode ('utf-8') 8 bytes _res_size = client. recv (1024) # accept command length 9 print ('COMMAND result length: ', pai_res_size) 10 Export ed_size = 011 export ed_data = B '12 while export ed_size <int (partition _res_size.decode (): 13 data = client. recv (1024) 14 received_size + = len (data) # The number of records received each time starting to be less than 1024. Therefore, the len must judge 15 received_data + = data16 else: 17 print ('cmd res receive done... ', received_size) 18 print (received_data.decode () 19 client. close ()

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.