is it possible to send raw data through ftp?

Fredrik Lundh fredrik at
Fri Oct 20 07:10:23 CEST 2006

daniel wrote:

> well, I'm trying to use ftplib to upload data that received from
> socket, and the application is required to  restart the transfer at a
> specific interval so as to generate a different target file on the
> server to store subsequent data.

> the problem is 'storbinary' accepts only file-like object

"storbinary" works with anything that has a "read" method that takes a 
buffer size, so you can wrap the source stream in something like:

# File wrapper that reads no more than 'bytes' data from a stream. Sets
# the 'eof' attribute to true if the stream ends.

class FileWrapper:
     def __init__(self, fp, bytes):
	self.fp = fp
	self.bytes = bytes
         self.eof = False
     def read(self, bufsize):
	if self.bytes <= 0:
	    return ""
	data =, self.bytes))
         if not data:
             self.eof = True
	self.bytes -= len(data)
	return data

source = open(...)

while 1:
     cmd = "STOR " + generate_file_name()
     f = FileWrapper(source, 1000000)
     ftp.storbinary(cmd, f)
     if f.eof:


More information about the Python-list mailing list