This article mainly introduces the python: socket transmission large file example, which has some reference value. if you are interested, you can understand it. This article mainly introduces the python: socket transmission large file example, it has some reference value. if you are interested, you can take a look at it,
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 help you learn and support PHP.
For more python: articles related to socket transmission of large file examples, please follow the PHP Chinese network!