By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

is it possible to send raw data through ftp?

P: n/a
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, I have to
use socketobj.makefile() to do that, how can I stop and resume that
transfer then? the abort() generates lots of wierd and unexpected
behavior, I guess if there is a way to upload raw data buffer, the
restart action should be implemented more easily.

thanks.

daniel

Oct 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.fp.read(min(bufsize, 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:
break

</F>

Oct 20 '06 #2

P: n/a


On Oct 20, 1:10 pm, Fredrik Lundh <fred...@pythonware.comwrote:
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.fp.read(min(bufsize, 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:
break

</F>
I'm trying to understand your code, thank you so much for the help.

daniel

Oct 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.