Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 648bcd7

Browse files
committed
Merged revisions 76546 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r76546 | antoine.pitrou | 2009-11-27 14:18:34 +0100 (ven., 27 nov. 2009) | 7 lines 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()) ........
1 parent 1a305fd commit 648bcd7

4 files changed

Lines changed: 44 additions & 7 deletions

File tree

Doc/library/ftplib.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,18 @@ followed by ``lines`` for the text version or ``binary`` for the binary version.
226226
Passive mode is on by default.
227227

228228

229-
.. method:: FTP.storbinary(cmd, file, blocksize=8192, callback=None)
229+
.. method:: FTP.storbinary(cmd, file, blocksize=8192, callback=None, rest=None)
230230

231231
Store a file in binary transfer mode. *cmd* should be an appropriate
232232
``STOR`` command: ``"STOR filename"``. *file* is an open file object which is
233233
read until EOF using its :meth:`read` method in blocks of size *blocksize* to
234234
provide the data to be stored. The *blocksize* argument defaults to 8192.
235235
*callback* is an optional single parameter callable that is called
236-
on each block of data after it is sent.
236+
on each block of data after it is sent. *rest* means the same thing as in
237+
the :meth:`transfercmd` method.
238+
239+
.. versionchanged:: 3.2
240+
*rest* parameter added.
237241

238242

239243
.. method:: FTP.storlines(cmd, file, callback=None)

Lib/ftplib.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def retrlines(self, cmd, callback = None):
433433
conn.close()
434434
return self.voidresp()
435435

436-
def storbinary(self, cmd, fp, blocksize=8192, callback=None):
436+
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
437437
"""Store a file in binary mode. A new port is created for you.
438438
439439
Args:
@@ -443,12 +443,13 @@ def storbinary(self, cmd, fp, blocksize=8192, callback=None):
443443
the connection at once. [default: 8192]
444444
callback: An optional single parameter callable that is called on
445445
on each block of data after it is sent. [default: None]
446+
rest: Passed to transfercmd(). [default: None]
446447
447448
Returns:
448449
The response code.
449450
"""
450451
self.voidcmd('TYPE I')
451-
conn = self.transfercmd(cmd)
452+
conn = self.transfercmd(cmd, rest)
452453
while 1:
453454
buf = fp.read(blocksize)
454455
if not buf: break
@@ -714,9 +715,9 @@ def retrlines(self, cmd, callback = None):
714715
conn.close()
715716
return self.voidresp()
716717

717-
def storbinary(self, cmd, fp, blocksize=8192, callback=None):
718+
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
718719
self.voidcmd('TYPE I')
719-
conn = self.transfercmd(cmd)
720+
conn = self.transfercmd(cmd, rest)
720721
try:
721722
while 1:
722723
buf = fp.read(blocksize)

Lib/test/test_ftplib.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self, conn):
5757
self.last_received_cmd = None
5858
self.last_received_data = ''
5959
self.next_response = ''
60+
self.rest = None
6061
self.push('220 welcome')
6162

6263
def collect_incoming_data(self, data):
@@ -170,10 +171,19 @@ def cmd_quit(self, arg):
170171
def cmd_stor(self, arg):
171172
self.push('125 stor ok')
172173

174+
def cmd_rest(self, arg):
175+
self.rest = arg
176+
self.push('350 rest ok')
177+
173178
def cmd_retr(self, arg):
174179
self.push('125 retr ok')
175-
self.dtp.push(RETR_DATA)
180+
if self.rest is not None:
181+
offset = int(self.rest)
182+
else:
183+
offset = 0
184+
self.dtp.push(RETR_DATA[offset:])
176185
self.dtp.close_when_done()
186+
self.rest = None
177187

178188
def cmd_list(self, arg):
179189
self.push('125 list ok')
@@ -450,6 +460,17 @@ def callback(data):
450460
self.client.retrbinary('retr', callback)
451461
self.assertEqual(''.join(received), RETR_DATA)
452462

463+
def test_retrbinary_rest(self):
464+
def callback(data):
465+
received.append(data.decode('ascii'))
466+
for rest in (0, 10, 20):
467+
received = []
468+
self.client.retrbinary('retr', callback, rest=rest)
469+
self.assertEqual(''.join(received), RETR_DATA[rest:],
470+
msg='rest test case %d %d %d' % (rest,
471+
len(''.join(received)),
472+
len(RETR_DATA[rest:])))
473+
453474
def test_retrlines(self):
454475
received = []
455476
self.client.retrlines('retr', received.append)
@@ -465,6 +486,13 @@ def test_storbinary(self):
465486
self.client.storbinary('stor', f, callback=lambda x: flag.append(None))
466487
self.assertTrue(flag)
467488

489+
def test_storbinary_rest(self):
490+
f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii'))
491+
for r in (30, '30'):
492+
f.seek(0)
493+
self.client.storbinary('stor', f, rest=r)
494+
self.assertEqual(self.server.handler.rest, str(r))
495+
468496
def test_storlines(self):
469497
f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii'))
470498
self.client.storlines('stor', f)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ C-API
140140
Library
141141
-------
142142

143+
- Issue #6845: Add restart support for binary upload in ftplib. The
144+
`storbinary()` method of FTP and FTP_TLS objects gains an optional `rest`
145+
argument. Patch by Pablo Mouzo.
146+
143147
- Issue #5788: `datetime.timedelta` objects get a new `total_seconds()`
144148
method returning the total number of seconds in the duration. Patch by
145149
Brian Quinlan.

0 commit comments

Comments
 (0)