Home > other >  Python3 socket programming transfer large files, while loop termination without cause
Python3 socket programming transfer large files, while loop termination without cause

Time:11-21

The following is a function of a sending files, transmission parameters is the file name and socket
Generate a compressed package, complete package will generate package again after send out,
When sending a few small files (a few KB) is normal, as the chart


Exit the while loop will determine conditions to false (normal)
But it is sent more than 2 MB file, will this


Haven't sent out, the condition is true, it jumped out of the

And the back of the
Print (" bytes sent a total of % s "% send_size)
Print (" Send: file sent successfully ")
Did not perform,

Why the condition is true, it suddenly jump out?

The source code is as follows:
 def file_send (self, conn, filename) : 
True_name=filename
Filepath=self. Host_path_list [filename]
# filepath is equivalent to a URL

If filename. The find (')!=1:
Filename=filename [0: filename. The find ('. ')) + "zip"
The else:
Filename=filename + ". Zip "

Z=zipfile. Zipfile (filename, 'w')
# to create a new object zip

If OS. Path. Isdir (filepath) :
# if the query is a folder compression is put all the things into the filename. Zip
For d in OS. Listdir (filepath) :
Z.w rite (filepath + OS. Sep + d, d)
The else:
# find file is only pressure a line
Z.w rite (filepath, true_name)
Z.c lose ()

# header information encapsulation into json encoded into bytes
Header_dic={
'filename' : filename,
'file_size: z.i nfolist () [0]. File_size
}
Header_json=json. Dumps (header_dic)
Header_bytes=header_json. Encode (' utf-8)

# packaging file header
The # header encapsulated into the length of the tuple
Conn. Send (struct. Pack (' I ', len (header_bytes)))

# send head
Conn. Send (header_bytes)

Total_size=z.i nfolist () [0]. File_size

# to send data
With the open (filename, 'rb +') as f:
Send_size=0
While send_size & lt;=total_size:
Data=https://bbs.csdn.net/topics/f.read (1024)
Conn. Send (data)
Send_size +=len (data)
Print (byte + "has been sent: % s % s" % (send_size, (send_size & lt;=total_size)))
Print (" bytes sent a total of % s "% send_size)
Print (" Send: file sent successfully ")
While True:
# used to delete the generated package before
Try:
OS. Remove (filename)
Break
Except:
The continue

  • Related