Issue #6845: Add restart support for binary upload in ftplib. The

`storbinary()` method of FTP and FTP_TLS objects gains an optional `rest`
argument.  Patch by Pablo Mouzo.

(note: the patch also adds a test for the rest argument in retrbinary())
This commit is contained in:
Antoine Pitrou 2009-11-27 13:18:34 +00:00
parent 2600a33219
commit acbe3bdbab
4 changed files with 41 additions and 7 deletions

View File

@ -233,14 +233,15 @@ followed by ``lines`` for the text version or ``binary`` for the binary version.
it is on by default.) it is on by default.)
.. method:: FTP.storbinary(command, file[, blocksize, callback]) .. method:: FTP.storbinary(command, file[, blocksize, callback, rest])
Store a file in binary transfer mode. *command* should be an appropriate Store a file in binary transfer mode. *command* should be an appropriate
``STOR`` command: ``"STOR filename"``. *file* is an open file object which is ``STOR`` command: ``"STOR filename"``. *file* is an open file object which is
read until EOF using its :meth:`read` method in blocks of size *blocksize* to read until EOF using its :meth:`read` method in blocks of size *blocksize* to
provide the data to be stored. The *blocksize* argument defaults to 8192. provide the data to be stored. The *blocksize* argument defaults to 8192.
*callback* is an optional single parameter callable that is called *callback* is an optional single parameter callable that is called
on each block of data after it is sent. on each block of data after it is sent. *rest* means the same thing as in
the :meth:`transfercmd` method.
.. versionchanged:: 2.1 .. versionchanged:: 2.1
default for *blocksize* added. default for *blocksize* added.
@ -248,6 +249,8 @@ followed by ``lines`` for the text version or ``binary`` for the binary version.
.. versionchanged:: 2.6 .. versionchanged:: 2.6
*callback* parameter added. *callback* parameter added.
.. versionchanged:: 2.7
*rest* parameter added.
.. method:: FTP.storlines(command, file[, callback]) .. method:: FTP.storlines(command, file[, callback])

View File

@ -431,7 +431,7 @@ class FTP:
conn.close() conn.close()
return self.voidresp() return self.voidresp()
def storbinary(self, cmd, fp, blocksize=8192, callback=None): def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
"""Store a file in binary mode. A new port is created for you. """Store a file in binary mode. A new port is created for you.
Args: Args:
@ -441,12 +441,13 @@ class FTP:
the connection at once. [default: 8192] the connection at once. [default: 8192]
callback: An optional single parameter callable that is called on callback: An optional single parameter callable that is called on
on each block of data after it is sent. [default: None] on each block of data after it is sent. [default: None]
rest: Passed to transfercmd(). [default: None]
Returns: Returns:
The response code. The response code.
""" """
self.voidcmd('TYPE I') self.voidcmd('TYPE I')
conn = self.transfercmd(cmd) conn = self.transfercmd(cmd, rest)
while 1: while 1:
buf = fp.read(blocksize) buf = fp.read(blocksize)
if not buf: break if not buf: break
@ -712,9 +713,9 @@ else:
conn.close() conn.close()
return self.voidresp() return self.voidresp()
def storbinary(self, cmd, fp, blocksize=8192, callback=None): def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
self.voidcmd('TYPE I') self.voidcmd('TYPE I')
conn = self.transfercmd(cmd) conn = self.transfercmd(cmd, rest)
try: try:
while 1: while 1:
buf = fp.read(blocksize) buf = fp.read(blocksize)

View File

@ -55,6 +55,7 @@ class DummyFTPHandler(asynchat.async_chat):
self.last_received_cmd = None self.last_received_cmd = None
self.last_received_data = '' self.last_received_data = ''
self.next_response = '' self.next_response = ''
self.rest = None
self.push('220 welcome') self.push('220 welcome')
def collect_incoming_data(self, data): def collect_incoming_data(self, data):
@ -168,10 +169,19 @@ class DummyFTPHandler(asynchat.async_chat):
def cmd_stor(self, arg): def cmd_stor(self, arg):
self.push('125 stor ok') self.push('125 stor ok')
def cmd_rest(self, arg):
self.rest = arg
self.push('350 rest ok')
def cmd_retr(self, arg): def cmd_retr(self, arg):
self.push('125 retr ok') self.push('125 retr ok')
self.dtp.push(RETR_DATA) if self.rest is not None:
offset = int(self.rest)
else:
offset = 0
self.dtp.push(RETR_DATA[offset:])
self.dtp.close_when_done() self.dtp.close_when_done()
self.rest = None
def cmd_list(self, arg): def cmd_list(self, arg):
self.push('125 list ok') self.push('125 list ok')
@ -444,6 +454,15 @@ class TestFTPClass(TestCase):
self.client.retrbinary('retr', received.append) self.client.retrbinary('retr', received.append)
self.assertEqual(''.join(received), RETR_DATA) self.assertEqual(''.join(received), RETR_DATA)
def test_retrbinary_rest(self):
for rest in (0, 10, 20):
received = []
self.client.retrbinary('retr', received.append, rest=rest)
self.assertEqual(''.join(received), RETR_DATA[rest:],
msg='rest test case %d %d %d' % (rest,
len(''.join(received)),
len(RETR_DATA[rest:])))
def test_retrlines(self): def test_retrlines(self):
received = [] received = []
self.client.retrlines('retr', received.append) self.client.retrlines('retr', received.append)
@ -459,6 +478,13 @@ class TestFTPClass(TestCase):
self.client.storbinary('stor', f, callback=lambda x: flag.append(None)) self.client.storbinary('stor', f, callback=lambda x: flag.append(None))
self.assertTrue(flag) self.assertTrue(flag)
def test_storbinary_rest(self):
f = StringIO.StringIO(RETR_DATA)
for r in (30, '30'):
f.seek(0)
self.client.storbinary('stor', f, rest=r)
self.assertEqual(self.server.handler.rest, str(r))
def test_storlines(self): def test_storlines(self):
f = StringIO.StringIO(RETR_DATA.replace('\r\n', '\n')) f = StringIO.StringIO(RETR_DATA.replace('\r\n', '\n'))
self.client.storlines('stor', f) self.client.storlines('stor', f)

View File

@ -483,6 +483,10 @@ Core and Builtins
Library Library
------- -------
- Issue #6845: Add restart support for binary upload in ftplib. The
`storbinary()` method of FTP and FTP_TLS objects gains an optional `rest`
argument. Patch by Pablo Mouzo.
- Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` - Issue #5788: `datetime.timedelta` objects get a new `total_seconds()`
method returning the total number of seconds in the duration. Patch by method returning the total number of seconds in the duration. Patch by
Brian Quinlan. Brian Quinlan.