Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 1 | # An FTP client class. Based on RFC 959: File Transfer Protocol |
| 2 | # (FTP), by J. Postel and J. Reynolds |
| 3 | |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 4 | # Changes and improvements suggested by Steve Majewski |
Jack Jansen | 40b9835 | 1995-01-19 12:24:45 +0000 | [diff] [blame] | 5 | # Modified by Jack to work on the mac. |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 8 | # Example: |
| 9 | # |
| 10 | # >>> from ftplib import FTP |
Guido van Rossum | c0e68d1 | 1995-09-30 16:51:50 +0000 | [diff] [blame] | 11 | # >>> ftp = FTP('ftp.python.org') # connect to host, default port |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 12 | # >>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13 | # >>> ftp.retrlines('LIST') # list directory contents |
Guido van Rossum | c0e68d1 | 1995-09-30 16:51:50 +0000 | [diff] [blame] | 14 | # total 9 |
| 15 | # drwxr-xr-x 8 root wheel 1024 Jan 3 1994 . |
| 16 | # drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .. |
| 17 | # drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin |
| 18 | # drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc |
| 19 | # d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming |
| 20 | # drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib |
| 21 | # drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub |
| 22 | # drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr |
| 23 | # -rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 24 | # >>> ftp.quit() |
Guido van Rossum | c0e68d1 | 1995-09-30 16:51:50 +0000 | [diff] [blame] | 25 | # >>> |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 26 | # |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 27 | # To download a file, use ftp.retrlines('RETR ' + filename), |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 28 | # or ftp.retrbinary() with slightly different arguments. |
| 29 | # To upload a file, use ftp.storlines() or ftp.storbinary(), which have |
Guido van Rossum | c0e68d1 | 1995-09-30 16:51:50 +0000 | [diff] [blame] | 30 | # an open file as argument (see their definitions below for details). |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 31 | # The download/upload functions first issue appropriate TYPE and PORT |
| 32 | # commands. |
| 33 | |
| 34 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 35 | import os |
| 36 | import sys |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 37 | import string |
| 38 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 39 | # Import SOCKS module if it exists, else standard socket module socket |
| 40 | try: |
| 41 | import SOCKS; socket = SOCKS |
| 42 | except ImportError: |
| 43 | import socket |
| 44 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 46 | # Magic number from <socket.h> |
| 47 | MSG_OOB = 0x1 # Process data out of band |
| 48 | |
| 49 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 50 | # The standard FTP server control port |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 51 | FTP_PORT = 21 |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 52 | |
| 53 | |
Guido van Rossum | 2197479 | 1992-11-06 13:34:17 +0000 | [diff] [blame] | 54 | # Exception raised when an error or invalid response is received |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 55 | error_reply = 'ftplib.error_reply' # unexpected [123]xx reply |
| 56 | error_temp = 'ftplib.error_temp' # 4xx errors |
| 57 | error_perm = 'ftplib.error_perm' # 5xx errors |
| 58 | error_proto = 'ftplib.error_proto' # response does not begin with [1-5] |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 59 | |
| 60 | |
Guido van Rossum | 2197479 | 1992-11-06 13:34:17 +0000 | [diff] [blame] | 61 | # All exceptions (hopefully) that may be raised here and that aren't |
| 62 | # (always) programming errors on our side |
| 63 | all_errors = (error_reply, error_temp, error_perm, error_proto, \ |
Guido van Rossum | c0e68d1 | 1995-09-30 16:51:50 +0000 | [diff] [blame] | 64 | socket.error, IOError, EOFError) |
Guido van Rossum | 2197479 | 1992-11-06 13:34:17 +0000 | [diff] [blame] | 65 | |
| 66 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 67 | # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) |
| 68 | CRLF = '\r\n' |
| 69 | |
| 70 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 71 | # The class itself |
| 72 | class FTP: |
| 73 | |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 74 | # New initialization method (called by class instantiation) |
| 75 | # Initialize host to localhost, port to standard ftp port |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 76 | # Optional arguments are host (for connect()), |
| 77 | # and user, passwd, acct (for login()) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | def __init__(self, host = '', user = '', passwd = '', acct = ''): |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 79 | # Initialize the instance to something mostly harmless |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 80 | self.debugging = 0 |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 81 | self.host = '' |
| 82 | self.port = FTP_PORT |
| 83 | self.sock = None |
| 84 | self.file = None |
| 85 | self.welcome = None |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 86 | if host: |
| 87 | self.connect(host) |
| 88 | if user: self.login(user, passwd, acct) |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 90 | # Connect to host. Arguments: |
| 91 | # - host: hostname to connect to (default previous host) |
| 92 | # - port: port to connect to (default previous port) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 93 | def connect(self, host = '', port = 0): |
| 94 | if host: self.host = host |
| 95 | if port: self.port = port |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 96 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 97 | self.sock.connect(self.host, self.port) |
| 98 | self.file = self.sock.makefile('r') |
| 99 | self.welcome = self.getresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 100 | |
| 101 | # Get the welcome message from the server |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 102 | # (this is read and squirreled away by connect()) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 103 | def getwelcome(self): |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 104 | if self.debugging: |
| 105 | print '*welcome*', self.sanitize(self.welcome) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 106 | return self.welcome |
| 107 | |
| 108 | # Set the debugging level. Argument level means: |
| 109 | # 0: no debugging output (default) |
| 110 | # 1: print commands and responses but not body text etc. |
| 111 | # 2: also print raw lines read and sent before stripping CR/LF |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 112 | def set_debuglevel(self, level): |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 113 | self.debugging = level |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 114 | debug = set_debuglevel |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 116 | # Internal: "sanitize" a string for printing |
| 117 | def sanitize(self, s): |
| 118 | if s[:5] == 'pass ' or s[:5] == 'PASS ': |
| 119 | i = len(s) |
| 120 | while i > 5 and s[i-1] in '\r\n': |
| 121 | i = i-1 |
| 122 | s = s[:5] + '*'*(i-5) + s[i:] |
| 123 | return `s` |
| 124 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 125 | # Internal: send one line to the server, appending CRLF |
| 126 | def putline(self, line): |
| 127 | line = line + CRLF |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 128 | if self.debugging > 1: print '*put*', self.sanitize(line) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 129 | self.sock.send(line) |
| 130 | |
| 131 | # Internal: send one command to the server (through putline()) |
| 132 | def putcmd(self, line): |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 133 | if self.debugging: print '*cmd*', self.sanitize(line) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 134 | self.putline(line) |
| 135 | |
| 136 | # Internal: return one line from the server, stripping CRLF. |
| 137 | # Raise EOFError if the connection is closed |
| 138 | def getline(self): |
| 139 | line = self.file.readline() |
| 140 | if self.debugging > 1: |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 141 | print '*get*', self.sanitize(line) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 142 | if not line: raise EOFError |
| 143 | if line[-2:] == CRLF: line = line[:-2] |
| 144 | elif line[-1:] in CRLF: line = line[:-1] |
| 145 | return line |
| 146 | |
| 147 | # Internal: get a response from the server, which may possibly |
| 148 | # consist of multiple lines. Return a single string with no |
| 149 | # trailing CRLF. If the response consists of multiple lines, |
| 150 | # these are separated by '\n' characters in the string |
| 151 | def getmultiline(self): |
| 152 | line = self.getline() |
| 153 | if line[3:4] == '-': |
| 154 | code = line[:3] |
| 155 | while 1: |
| 156 | nextline = self.getline() |
| 157 | line = line + ('\n' + nextline) |
| 158 | if nextline[:3] == code and \ |
| 159 | nextline[3:4] <> '-': |
| 160 | break |
| 161 | return line |
| 162 | |
| 163 | # Internal: get a response from the server. |
| 164 | # Raise various errors if the response indicates an error |
| 165 | def getresp(self): |
| 166 | resp = self.getmultiline() |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 167 | if self.debugging: print '*resp*', self.sanitize(resp) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 168 | self.lastresp = resp[:3] |
| 169 | c = resp[:1] |
| 170 | if c == '4': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 171 | raise error_temp, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 172 | if c == '5': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 173 | raise error_perm, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 174 | if c not in '123': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 175 | raise error_proto, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 176 | return resp |
| 177 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 178 | # Expect a response beginning with '2' |
| 179 | def voidresp(self): |
| 180 | resp = self.getresp() |
| 181 | if resp[0] <> '2': |
| 182 | raise error_reply, resp |
| 183 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 184 | # Abort a file transfer. Uses out-of-band data. |
| 185 | # This does not follow the procedure from the RFC to send Telnet |
| 186 | # IP and Synch; that doesn't seem to work with the servers I've |
| 187 | # tried. Instead, just send the ABOR command as OOB data. |
| 188 | def abort(self): |
| 189 | line = 'ABOR' + CRLF |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 190 | if self.debugging > 1: print '*put urgent*', self.sanitize(line) |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 191 | self.sock.send(line, MSG_OOB) |
| 192 | resp = self.getmultiline() |
| 193 | if resp[:3] not in ('426', '226'): |
| 194 | raise error_proto, resp |
| 195 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 196 | # Send a command and return the response |
| 197 | def sendcmd(self, cmd): |
| 198 | self.putcmd(cmd) |
| 199 | return self.getresp() |
| 200 | |
Guido van Rossum | c68a401 | 1992-11-05 23:01:42 +0000 | [diff] [blame] | 201 | # Send a command and expect a response beginning with '2' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 202 | def voidcmd(self, cmd): |
Guido van Rossum | c68a401 | 1992-11-05 23:01:42 +0000 | [diff] [blame] | 203 | self.putcmd(cmd) |
| 204 | self.voidresp() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 206 | # Send a PORT command with the current host and the given port number |
Guido van Rossum | 221ec0b | 1995-08-04 04:39:30 +0000 | [diff] [blame] | 207 | def sendport(self, host, port): |
| 208 | hbytes = string.splitfields(host, '.') |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 209 | pbytes = [`port/256`, `port%256`] |
| 210 | bytes = hbytes + pbytes |
| 211 | cmd = 'PORT ' + string.joinfields(bytes, ',') |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 212 | self.voidcmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 213 | |
| 214 | # Create a new socket and send a PORT command for it |
| 215 | def makeport(self): |
| 216 | global nextport |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 217 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | 303c179 | 1995-06-20 17:21:42 +0000 | [diff] [blame] | 218 | sock.bind(('', 0)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | sock.listen(1) |
Guido van Rossum | 221ec0b | 1995-08-04 04:39:30 +0000 | [diff] [blame] | 220 | dummyhost, port = sock.getsockname() # Get proper port |
| 221 | host, dummyport = self.sock.getsockname() # Get proper host |
| 222 | resp = self.sendport(host, port) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 223 | return sock |
| 224 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 225 | # Send a port command and a transfer command, accept the connection |
| 226 | # and return the socket for the connection |
| 227 | def transfercmd(self, cmd): |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 228 | sock = self.makeport() |
| 229 | resp = self.sendcmd(cmd) |
| 230 | if resp[0] <> '1': |
| 231 | raise error_reply, resp |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 232 | conn, sockaddr = sock.accept() |
| 233 | return conn |
| 234 | |
| 235 | # Login, default anonymous |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | def login(self, user = '', passwd = '', acct = ''): |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 237 | if not user: user = 'anonymous' |
| 238 | if user == 'anonymous' and passwd in ('', '-'): |
| 239 | thishost = socket.gethostname() |
Jack Jansen | 2db6bfc | 1995-05-04 15:02:18 +0000 | [diff] [blame] | 240 | # Make sure it is fully qualified |
| 241 | if not '.' in thishost: |
| 242 | thisaddr = socket.gethostbyname(thishost) |
Guido van Rossum | 303c179 | 1995-06-20 17:21:42 +0000 | [diff] [blame] | 243 | firstname, names, unused = \ |
| 244 | socket.gethostbyaddr(thisaddr) |
| 245 | names.insert(0, firstname) |
| 246 | for name in names: |
| 247 | if '.' in name: |
| 248 | thishost = name |
| 249 | break |
Jack Jansen | 40b9835 | 1995-01-19 12:24:45 +0000 | [diff] [blame] | 250 | try: |
| 251 | if os.environ.has_key('LOGNAME'): |
| 252 | realuser = os.environ['LOGNAME'] |
| 253 | elif os.environ.has_key('USER'): |
| 254 | realuser = os.environ['USER'] |
| 255 | else: |
| 256 | realuser = 'anonymous' |
| 257 | except AttributeError: |
| 258 | # Not all systems have os.environ.... |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 259 | realuser = 'anonymous' |
| 260 | passwd = passwd + realuser + '@' + thishost |
| 261 | resp = self.sendcmd('USER ' + user) |
| 262 | if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd) |
| 263 | if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct) |
| 264 | if resp[0] <> '2': |
| 265 | raise error_reply, resp |
| 266 | |
| 267 | # Retrieve data in binary mode. |
| 268 | # The argument is a RETR command. |
| 269 | # The callback function is called for each block. |
| 270 | # This creates a new port for you |
| 271 | def retrbinary(self, cmd, callback, blocksize): |
| 272 | self.voidcmd('TYPE I') |
| 273 | conn = self.transfercmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 274 | while 1: |
| 275 | data = conn.recv(blocksize) |
| 276 | if not data: |
| 277 | break |
| 278 | callback(data) |
| 279 | conn.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 280 | self.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 282 | # Retrieve data in line mode. |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 283 | # The argument is a RETR or LIST command. |
| 284 | # The callback function is called for each line, with trailing |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 285 | # CRLF stripped. This creates a new port for you. |
| 286 | # print_lines is the default callback |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | def retrlines(self, cmd, callback = None): |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 288 | if not callback: callback = print_line |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 289 | resp = self.sendcmd('TYPE A') |
| 290 | conn = self.transfercmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 291 | fp = conn.makefile('r') |
| 292 | while 1: |
| 293 | line = fp.readline() |
Guido van Rossum | c0e68d1 | 1995-09-30 16:51:50 +0000 | [diff] [blame] | 294 | if self.debugging > 2: print '*retr*', `line` |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 295 | if not line: |
| 296 | break |
| 297 | if line[-2:] == CRLF: |
| 298 | line = line[:-2] |
| 299 | elif line[:-1] == '\n': |
| 300 | line = line[:-1] |
| 301 | callback(line) |
| 302 | fp.close() |
| 303 | conn.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 304 | self.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 306 | # Store a file in binary mode |
| 307 | def storbinary(self, cmd, fp, blocksize): |
| 308 | self.voidcmd('TYPE I') |
| 309 | conn = self.transfercmd(cmd) |
| 310 | while 1: |
| 311 | buf = fp.read(blocksize) |
| 312 | if not buf: break |
| 313 | conn.send(buf) |
| 314 | conn.close() |
| 315 | self.voidresp() |
| 316 | |
| 317 | # Store a file in line mode |
| 318 | def storlines(self, cmd, fp): |
| 319 | self.voidcmd('TYPE A') |
| 320 | conn = self.transfercmd(cmd) |
| 321 | while 1: |
| 322 | buf = fp.readline() |
| 323 | if not buf: break |
| 324 | if buf[-2:] <> CRLF: |
| 325 | if buf[-1] in CRLF: buf = buf[:-1] |
| 326 | buf = buf + CRLF |
| 327 | conn.send(buf) |
| 328 | conn.close() |
| 329 | self.voidresp() |
| 330 | |
| 331 | # Return a list of files in a given directory (default the current) |
| 332 | def nlst(self, *args): |
| 333 | cmd = 'NLST' |
| 334 | for arg in args: |
| 335 | cmd = cmd + (' ' + arg) |
| 336 | files = [] |
| 337 | self.retrlines(cmd, files.append) |
| 338 | return files |
| 339 | |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 340 | # List a directory in long form. By default list current directory |
| 341 | # to stdout. Optional last argument is callback function; |
| 342 | # all non-empty arguments before it are concatenated to the |
| 343 | # LIST command. (This *should* only be used for a pathname.) |
| 344 | def dir(self, *args): |
| 345 | cmd = 'LIST' |
| 346 | func = None |
| 347 | if args[-1:] and type(args[-1]) != type(''): |
| 348 | args, func = args[:-1], args[-1] |
| 349 | for arg in args: |
| 350 | if arg: |
| 351 | cmd = cmd + (' ' + arg) |
| 352 | self.retrlines(cmd, func) |
| 353 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 354 | # Rename a file |
| 355 | def rename(self, fromname, toname): |
| 356 | resp = self.sendcmd('RNFR ' + fromname) |
| 357 | if resp[0] <> '3': |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 358 | raise error_reply, resp |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 359 | self.voidcmd('RNTO ' + toname) |
| 360 | |
Guido van Rossum | 02cf582 | 1993-05-17 08:00:02 +0000 | [diff] [blame] | 361 | # Change to a directory |
| 362 | def cwd(self, dirname): |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 363 | if dirname == '..': |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 364 | try: |
| 365 | self.voidcmd('CDUP') |
| 366 | return |
| 367 | except error_perm, msg: |
| 368 | if msg[:3] != '500': |
| 369 | raise error_perm, msg |
| 370 | cmd = 'CWD ' + dirname |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 371 | self.voidcmd(cmd) |
Guido van Rossum | 02cf582 | 1993-05-17 08:00:02 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 373 | # Retrieve the size of a file |
| 374 | def size(self, filename): |
| 375 | resp = self.sendcmd('SIZE ' + filename) |
| 376 | if resp[:3] == '213': |
| 377 | return string.atoi(string.strip(resp[3:])) |
| 378 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 379 | # Make a directory, return its full pathname |
| 380 | def mkd(self, dirname): |
| 381 | resp = self.sendcmd('MKD ' + dirname) |
| 382 | return parse257(resp) |
| 383 | |
| 384 | # Return current wording directory |
| 385 | def pwd(self): |
| 386 | resp = self.sendcmd('PWD') |
| 387 | return parse257(resp) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 388 | |
| 389 | # Quit, and close the connection |
| 390 | def quit(self): |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 391 | self.voidcmd('QUIT') |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 392 | self.close() |
| 393 | |
| 394 | # Close the connection without assuming anything about it |
| 395 | def close(self): |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 396 | self.file.close() |
| 397 | self.sock.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 398 | del self.file, self.sock |
| 399 | |
| 400 | |
| 401 | # Parse a response type 257 |
| 402 | def parse257(resp): |
| 403 | if resp[:3] <> '257': |
| 404 | raise error_reply, resp |
| 405 | if resp[3:5] <> ' "': |
| 406 | return '' # Not compliant to RFC 959, but UNIX ftpd does this |
| 407 | dirname = '' |
| 408 | i = 5 |
| 409 | n = len(resp) |
| 410 | while i < n: |
| 411 | c = resp[i] |
| 412 | i = i+1 |
| 413 | if c == '"': |
| 414 | if i >= n or resp[i] <> '"': |
| 415 | break |
| 416 | i = i+1 |
| 417 | dirname = dirname + c |
| 418 | return dirname |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 420 | # Default retrlines callback to print a line |
| 421 | def print_line(line): |
| 422 | print line |
| 423 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 424 | |
| 425 | # Test program. |
| 426 | # Usage: ftp [-d] host [-l[dir]] [-d[dir]] [file] ... |
| 427 | def test(): |
| 428 | import marshal |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 429 | debugging = 0 |
| 430 | while sys.argv[1] == '-d': |
| 431 | debugging = debugging+1 |
| 432 | del sys.argv[1] |
| 433 | host = sys.argv[1] |
| 434 | ftp = FTP(host) |
| 435 | ftp.set_debuglevel(debugging) |
| 436 | ftp.login() |
| 437 | for file in sys.argv[2:]: |
| 438 | if file[:2] == '-l': |
| 439 | ftp.dir(file[2:]) |
| 440 | elif file[:2] == '-d': |
| 441 | cmd = 'CWD' |
| 442 | if file[2:]: cmd = cmd + ' ' + file[2:] |
| 443 | resp = ftp.sendcmd(cmd) |
| 444 | else: |
| 445 | ftp.retrbinary('RETR ' + file, \ |
| 446 | sys.stdout.write, 1024) |
| 447 | ftp.quit() |
Guido van Rossum | 221ec0b | 1995-08-04 04:39:30 +0000 | [diff] [blame] | 448 | |
| 449 | |
| 450 | if __name__ == '__main__': |
| 451 | test() |