Python: socket transmission large file example,

Source: Internet
Author: User

Python: socket transmission large file example,

Files can be transmitted, but compared with the files before and after transmission: socket_test.txt, there are some inconsistent server code at the end:

#! /Usr/bin/python #-*-coding: UTF-8-*-import sysreload (sys) sys. setdefaultencoding ("UTF-8") import time ''' wait for the connection to wait for the sending file to read the data and write the data to the file and save the data waiting for connection ''' import socketimport threadingimport timeimport structdef function (newsock, address ): FILEINFO_SIZE = struct. calcsize ('128si') ''' defines the file size (including the file name and file size. 128 S represents char [] (file name), And I represents an integer or long (file size) ''' while 1: try: fhead = newsock. recv (FILEINFO_SIZE) filename, filesize = struct. unpack ('128si', fhead) ''' unpacks the received database. According to the packaging rules 128si''' print "address is:", address print filename, len (filename), type (filename) print filesize # filename = 'new _ '+ filename. strip ('\ 00') # name the file transmitted by the new _ file filename = filename. strip ('\ 00') fp = open (filename, 'wb') # create a file and Prepare to write restsize = filesize print "recving... "while 1: if restsize> 102400: # if the remaining data packet is greater than 1024, the 1024 data packet filedata = newsock will be removed. recv (10240) else: filedata = newsock. recv (restsize) fp. write (filedata) # break if not filedata: break fp. write (filedata) restsize = restsize-len (filedata) # Calculate the remaining packet size if restsize <= 0: break fp. close () print "recv succeeded !! File named: ", filename encoding t Exception, e: print unicode (e ). encode ('gbk') print "the socket partner maybe closed" newsock. close () breaksock = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # create a tcp connection sock. bind ('10. 240.146.82 ', 8887) # specifies the port and ipsock. listen (5) # listen while True: newsock, address = sock. accept () print "accept another connection" tmpThread = threading. thread (target = function, args = (newsock, address) # if a file is received, create the tmpThread Thread. start () # execution thread print 'end'

Client code:

#! /Usr/bin/python #-*-coding: UTF-8-*-import sysreload (sys) sys. setdefaultencoding ("UTF-8") ''' input the file name and upload '''import socketimport timeimport structimport osf = open('socket_test.txt ', 'wb') for I in range (1000000): f. write ('for socket test, the line number is:' + str (I) + '\ n') f. close () sock = socket. socket (socket. AF_INET, socket. SOCK_STREAM) sock. settimeout (50) e = 0try: sock. connect ('10. 240.146.82 ', 8887) print 'connect... 'couldn't socket. timeout, e: print 'timeout', e0000t socket. error, e: print 'error', e0000t e: print 'any', eif not e: # while (1): # filename = raw_input ('input your filename -------> ') # input file name filename = 'socket_test.txt 'FILEINFO_SIZE = struct. calcsize ('128si') # encoding format size fhead = struct. pack ('128si', filename, OS. stat (filename ). st_size) # Pack sock according to the rules. send (fhead) # send File basic information data fp = open (filename, 'rb') fp2 = open('local_test.txt ', 'wb') I = 0 while 1: # Send File filedata = fp. read (10240) if not filedata: break sock. sendall (filedata) fp2.write (filedata) print I = I + 1 print "sending over... "fp. close () fp2.close ()

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.