Skip to content

Commit

Permalink
Return the error code from most commands, rather than swallowing it.
Browse files Browse the repository at this point in the history
Adapted the example (lying slightly about the string printed by
login()).
  • Loading branch information
gvanrossum committed Oct 7, 1997
1 parent ae590db commit 2f3941d
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions Lib/ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
>>> from ftplib import FTP
>>> ftp = FTP('ftp.python.org') # connect to host, default port
>>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname
'230 Guest login ok, access restrictions apply.'
>>> ftp.retrlines('LIST') # list directory contents
total 9
drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .
Expand All @@ -23,7 +24,9 @@
drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub
drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr
-rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg
'226 Transfer complete.'
>>> ftp.quit()
'221 Goodbye.'
>>>
A nice test that reveals some of the network dialogue would be:
Expand Down Expand Up @@ -98,9 +101,11 @@ def __init__(self, host = '', user = '', passwd = '', acct = ''):
self.sock = None
self.file = None
self.welcome = None
resp = None
if host:
self.connect(host)
if user: self.login(user, passwd, acct)
resp = self.connect(host)
if user: resp = self.login(user, passwd, acct)
return resp

def connect(self, host = '', port = 0):
'''Connect to host. Arguments are:
Expand All @@ -113,6 +118,7 @@ def connect(self, host = '', port = 0):
self.sock.connect(self.host, self.port)
self.file = self.sock.makefile('rb')
self.welcome = self.getresp()
return self.welcome

def getwelcome(self):
'''Get the welcome message from the server.
Expand Down Expand Up @@ -203,6 +209,7 @@ def voidresp(self):
resp = self.getresp()
if resp[0] <> '2':
raise error_reply, resp
return resp

def abort(self):
'''Abort a file transfer. Uses out-of-band data.
Expand All @@ -224,15 +231,15 @@ def sendcmd(self, cmd):
def voidcmd(self, cmd):
"""Send a command and expect a response beginning with '2'."""
self.putcmd(cmd)
self.voidresp()
return self.voidresp()

def sendport(self, host, port):
'''Send a PORT command with the current host and the given port number.'''
hbytes = string.splitfields(host, '.')
pbytes = [`port/256`, `port%256`]
bytes = hbytes + pbytes
cmd = 'PORT ' + string.joinfields(bytes, ',')
self.voidcmd(cmd)
return self.voidcmd(cmd)

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

def retrbinary(self, cmd, callback, blocksize):
'''Retrieve data in binary mode.
Expand All @@ -323,7 +331,7 @@ def retrbinary(self, cmd, callback, blocksize):
break
callback(data)
conn.close()
self.voidresp()
return self.voidresp()

def retrlines(self, cmd, callback = None):
'''Retrieve data in line mode.
Expand All @@ -347,7 +355,7 @@ def retrlines(self, cmd, callback = None):
callback(line)
fp.close()
conn.close()
self.voidresp()
return self.voidresp()

def storbinary(self, cmd, fp, blocksize):
'''Store a file in binary mode.'''
Expand All @@ -358,7 +366,7 @@ def storbinary(self, cmd, fp, blocksize):
if not buf: break
conn.send(buf)
conn.close()
self.voidresp()
return self.voidresp()

def storlines(self, cmd, fp):
'''Store a file in line mode.'''
Expand All @@ -372,12 +380,12 @@ def storlines(self, cmd, fp):
buf = buf + CRLF
conn.send(buf)
conn.close()
self.voidresp()
return self.voidresp()

def acct(self, password):
'''Send new account name.'''
cmd = 'ACCT ' + password
self.voidcmd(cmd)
return self.voidcmd(cmd)

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

def delete(self, filename):
'''Delete a file.'''
resp = self.sendcmd('DELE ' + filename)
if resp[:3] == '250':
return
return resp
elif resp[:1] == '5':
raise error_perm, resp
else:
Expand All @@ -424,13 +432,12 @@ def cwd(self, dirname):
'''Change to a directory.'''
if dirname == '..':
try:
self.voidcmd('CDUP')
return
return self.voidcmd('CDUP')
except error_perm, msg:
if msg[:3] != '500':
raise error_perm, msg
cmd = 'CWD ' + dirname
self.voidcmd(cmd)
return self.voidcmd(cmd)

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

def quit(self):
'''Quit, and close the connection.'''
self.voidcmd('QUIT')
resp = self.voidcmd('QUIT')
self.close()
return resp

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


def parse257(resp):
Expand All @@ -520,10 +527,12 @@ def parse257(resp):
dirname = dirname + c
return dirname


def print_line(line):
'''Default retrlines callback to print a line.'''
print line


def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
'''Copy file from one FTP-instance to another.'''
if not targetname: targetname = sourcename
Expand Down

0 comments on commit 2f3941d

Please sign in to comment.