8000 Return the error code from most commands, rather than swallowing it. · python/cpython@2f3941d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f3941d

Browse files
committed
Return the error code from most commands, rather than swallowing it.
Adapted the example (lying slightly about the string printed by login()).
1 parent ae590db commit 2f3941d

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

Lib/ftplib.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
>>> from ftplib import FTP
1313
>>> ftp = FTP('ftp.python.org') # connect to host, default port
1414
>>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname
15+
'230 Guest login ok, access restrictions apply.'
1516
>>> ftp.retrlines('LIST') # list directory contents
1617
total 9
1718
drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .
@@ -23,7 +24,9 @@
2324
drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub
2425
drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr
2526
-rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg
27+
'226 Transfer complete.'
2628
>>> ftp.quit()
29+
'221 Goodbye.'
2730
>>>
2831
2932
A nice test that reveals some of the network dialogue would be:
@@ -98,9 +101,11 @@ def __init__(self, host = '', user = '', passwd = '', acct = ''):
98101
self.sock = None
99102
self.file = None
100103
self.welcome = None
104+
resp = None
101105
if host:
102-
self.connect(host)
103-
if user: self.login(user, passwd, acct)
106+
resp = self.connect(host)
107+
if user: resp = self.login(user, passwd, acct)
108+
return resp
104109

105110
def connect(self, host = '', port = 0):
106111
'''Connect to host. Arguments are:
@@ -113,6 +118,7 @@ def connect(self, host = '', port = 0):
113118
self.sock.connect(self.host, self.port)
114119
self.file = self.sock.makefile('rb')
115120
self.welcome = self.getresp()
121+
return self.welcome
116122

117123
def getwelcome(self):
118124
'''Get the welcome message from the server.
@@ -203,6 +209,7 @@ def voidresp(self):
203209
resp = self.getresp()
204210
if resp[0] <> '2':
205211
raise error_reply, resp
212+
return resp
206213

207214
def abort(self):
208215
'''Abort a file transfer. Uses out-of-band data.
@@ -224,15 +231,15 @@ def sendcmd(self, cmd):
224231
def voidcmd(self, cmd):
225232
"""Send a command and expect a response beginning with '2'."""
226233
self.putcmd(cmd)
227-
self.voidresp()
234+
return self.voidresp()
228235

229236
def sendport(self, host, port):
230237
'''Send a PORT command with the current host and the given port number.'''
231238
hbytes = string.splitfields(host, '.')
232239
pbytes = [`port/256`, `port%256`]
233240
bytes = hbytes + pbytes
234241
cmd = 'PORT ' + string.joinfields(bytes, ',')
235-
self.voidcmd(cmd)
242+
return self.voidcmd(cmd)
236243

237244
def makeport(self):
238245
'''Create a new socket and send a PORT command for it.'''
@@ -309,6 +316,7 @@ def login(self, user = '', passwd = '', acct = ''):
309316
if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct)
310317
if resp[0] <> '2':
311318
raise error_reply, resp
319+
return resp
312320

313321
def retrbinary(self, cmd, callback, blocksize):
314322
'''Retrieve data in binary mode.
@@ -323,7 +331,7 @@ def retrbinary(self, cmd, callback, blocksize):
323331
break
324332
callback(data)
325333
conn.close()
326-
self.voidresp()
334+
return self.voidresp()
327335

328336
def retrlines(self, cmd, callback = None):
329337
'''Retrieve data in line mode.
@@ -347,7 +355,7 @@ def retrlines(self, cmd, callback = None):
347355
callback(line)
348356
fp.close()
349357
conn.close()
350-
self.voidresp()
358+
return self.voidresp()
351359

352360
def storbinary(self, cmd, fp, blocksize):
353361
'''Store a file in binary mode.'''
@@ -358,7 +366,7 @@ def storbinary(self, cmd, fp, blocksize):
358366
if not buf: break
359367
conn.send(buf)
360368
conn.close()
361-
self.voidresp()
369+
return self.voidresp()
362370

363371
def storlines(self, cmd, fp):
364372
'''Store a file in line mode.'''
@@ -372,12 +380,12 @@ def storlines(self, cmd, fp):
372380
buf = buf + CRLF
373381
conn.send(buf)
6D40 374382
conn.close()
375-
self.voidresp()
383+
return self.voidresp()
376384

377385
def acct(self, password):
378386
'''Send new account name.'''
379387
cmd = 'ACCT ' + password
380-
self.voidcmd(cmd)
388+
return self.voidcmd(cmd)
381389

382390
def nlst(self, *args):
383391
'''Return a list of files in a given directory (default the current).'''
@@ -408,13 +416,13 @@ def rename(self, fromname, toname):
408416
resp = self.sendcmd('RNFR ' + fromname)
409417
if resp[0] <> '3':
410418
raise error_reply, resp
411-
self.voidcmd('RNTO ' + toname)
419+
return self.voidcmd('RNTO ' + toname)
412420

413421
def delete(self, filename):
414422
'''Delete a file.'''
415423
resp = self.sendcmd('DELE ' + filename)
416424
if resp[:3] == '250':
417-
return
425+
return resp
418426
elif resp[:1] == '5':
419427
raise error_perm, resp
420428
else:
@@ -424,13 +432,12 @@ def cwd(self, dirname):
424432
'''Change to a directory.'''
425433
if dirname == '..':
426434
try:
427-
self.voidcmd('CDUP')
428-
return
435+
return self.voidcmd('CDUP')
429436
except error_perm, msg:
430437
if msg[:3] != '500':
431438
raise error_perm, msg
432439
cmd = 'CWD ' + dirname
433-
self.voidcmd(cmd)
440+
return self.voidcmd(cmd)
434441

435442
def size(self, filename):
436443
'''Retrieve the size of a file.'''
@@ -451,8 +458,9 @@ def pwd(self):
451458

452459
def quit(self):
453460
'''Quit, and close the connection.'''
454-
self.voidcmd('QUIT')
461+
resp = self.voidcmd('QUIT')
455462
self.close()
463+
return resp
456464

457465
def close(self):
458466
'''Close the connection without assuming anything about it.'''
@@ -495,7 +503,6 @@ def parse227(resp):
495503
host = string.join(numbers[:4], '.')
496504
port = (string.atoi(numbers[4]) << 8) + string.atoi(numbers[5])
497505
return host, port
498-
# end parse227
499506

500507

501508
def parse257(resp):
@@ -520,10 +527,12 @@ def parse257(resp):
520527
dirname = dirname + c
521528
return dirname
522529

530+
523531
def print_line(line):
524532
'''Default retrlines callback to print a line.'''
525533
print line
526534

535+
527536
def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
528537
'''Copy file from one FTP-instance to another.'''
529538
if not targetname: targetname = sourcename

0 commit comments

Comments
 (0)
0