034 File Upload

Source: Internet
Author: User

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

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.