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 | |
| 4 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 5 | # Example: |
| 6 | # |
| 7 | # >>> from ftplib import FTP |
| 8 | # >>> ftp = FTP().init('ftp.cwi.nl') # connect to host, default port |
| 9 | # >>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname |
| 10 | # >>> def handle_one_line(line): # callback for ftp.retrlines |
| 11 | # ... print line |
| 12 | # ... |
| 13 | # >>> ftp.retrlines('LIST', handle_one_line) # list directory contents |
| 14 | # total 43 |
| 15 | # d--x--x--x 2 root root 512 Jul 1 16:50 bin |
| 16 | # d--x--x--x 2 root root 512 Sep 16 1991 etc |
| 17 | # drwxr-xr-x 2 root ftp 10752 Sep 16 1991 lost+found |
| 18 | # drwxr-srwt 15 root ftp 10240 Nov 5 20:43 pub |
| 19 | # >>> ftp.quit() |
| 20 | # |
| 21 | # To download a file, use ftp.retrlines('RETR ' + filename, handle_one_line), |
| 22 | # or ftp.retrbinary() with slightly different arguments. |
| 23 | # To upload a file, use ftp.storlines() or ftp.storbinary(), which have |
| 24 | # an open file as argument. |
| 25 | # The download/upload functions first issue appropriate TYPE and PORT |
| 26 | # commands. |
| 27 | |
| 28 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 29 | import os |
| 30 | import sys |
| 31 | import socket |
| 32 | import string |
| 33 | |
| 34 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 35 | # The standard FTP server control port |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 36 | FTP_PORT = 21 |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | # Exception raiseds when an error or invalid response is received |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 40 | error_reply = 'ftplib.error_reply' # unexpected [123]xx reply |
| 41 | error_temp = 'ftplib.error_temp' # 4xx errors |
| 42 | error_perm = 'ftplib.error_perm' # 5xx errors |
| 43 | 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] | 44 | |
| 45 | |
| 46 | # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) |
| 47 | CRLF = '\r\n' |
| 48 | |
| 49 | |
| 50 | # Next port to be used by makeport(), with PORT_OFFSET added |
| 51 | nextport = 0 |
| 52 | PORT_OFFSET = 40000 |
| 53 | PORT_CYCLE = 1000 |
| 54 | # XXX This is a nuisance: when using the program several times in a row, |
| 55 | # reusing the port doesn't work and you have to edit the first port |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 56 | # assignment... We need getsockname()! |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | # The class itself |
| 60 | class FTP: |
| 61 | |
| 62 | # Initialize an instance. Arguments: |
| 63 | # - host: hostname to connect to |
| 64 | # - port: port to connect to (default the standard FTP port) |
| 65 | def init(self, host, *args): |
| 66 | if len(args) > 1: raise TypeError, 'too many args' |
| 67 | if args: port = args[0] |
| 68 | else: port = FTP_PORT |
| 69 | self.host = host |
| 70 | self.port = port |
| 71 | self.debugging = 0 |
| 72 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 73 | self.sock.connect(self.host, self.port) |
| 74 | self.file = self.sock.makefile('r') |
| 75 | self.welcome = self.getresp() |
| 76 | return self |
| 77 | |
| 78 | # Get the welcome message from the server |
| 79 | # (this is read and squirreled away by init()) |
| 80 | def getwelcome(self): |
| 81 | if self.debugging: print '*welcome*', `self.welcome` |
| 82 | return self.welcome |
| 83 | |
| 84 | # Set the debugging level. Argument level means: |
| 85 | # 0: no debugging output (default) |
| 86 | # 1: print commands and responses but not body text etc. |
| 87 | # 2: also print raw lines read and sent before stripping CR/LF |
| 88 | def debug(self, level): |
| 89 | self.debugging = level |
| 90 | |
| 91 | # Internal: send one line to the server, appending CRLF |
| 92 | def putline(self, line): |
| 93 | line = line + CRLF |
| 94 | if self.debugging > 1: print '*put*', `line` |
| 95 | self.sock.send(line) |
| 96 | |
| 97 | # Internal: send one command to the server (through putline()) |
| 98 | def putcmd(self, line): |
| 99 | if self.debugging: print '*cmd*', `line` |
| 100 | self.putline(line) |
| 101 | |
| 102 | # Internal: return one line from the server, stripping CRLF. |
| 103 | # Raise EOFError if the connection is closed |
| 104 | def getline(self): |
| 105 | line = self.file.readline() |
| 106 | if self.debugging > 1: |
| 107 | print '*get*', `line` |
| 108 | if not line: raise EOFError |
| 109 | if line[-2:] == CRLF: line = line[:-2] |
| 110 | elif line[-1:] in CRLF: line = line[:-1] |
| 111 | return line |
| 112 | |
| 113 | # Internal: get a response from the server, which may possibly |
| 114 | # consist of multiple lines. Return a single string with no |
| 115 | # trailing CRLF. If the response consists of multiple lines, |
| 116 | # these are separated by '\n' characters in the string |
| 117 | def getmultiline(self): |
| 118 | line = self.getline() |
| 119 | if line[3:4] == '-': |
| 120 | code = line[:3] |
| 121 | while 1: |
| 122 | nextline = self.getline() |
| 123 | line = line + ('\n' + nextline) |
| 124 | if nextline[:3] == code and \ |
| 125 | nextline[3:4] <> '-': |
| 126 | break |
| 127 | return line |
| 128 | |
| 129 | # Internal: get a response from the server. |
| 130 | # Raise various errors if the response indicates an error |
| 131 | def getresp(self): |
| 132 | resp = self.getmultiline() |
| 133 | if self.debugging: print '*resp*', `resp` |
| 134 | self.lastresp = resp[:3] |
| 135 | c = resp[:1] |
| 136 | if c == '4': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 137 | raise error_temp, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 138 | if c == '5': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 139 | raise error_perm, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 140 | if c not in '123': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 141 | raise error_proto, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 142 | return resp |
| 143 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 144 | # Expect a response beginning with '2' |
| 145 | def voidresp(self): |
| 146 | resp = self.getresp() |
| 147 | if resp[0] <> '2': |
| 148 | raise error_reply, resp |
| 149 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 150 | # Send a command and return the response |
| 151 | def sendcmd(self, cmd): |
| 152 | self.putcmd(cmd) |
| 153 | return self.getresp() |
| 154 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 155 | # Send a command and ignore the response, which must begin with '2' |
| 156 | def voidcmd(self, cmd): |
| 157 | resp = self.sendcmd(cmd) |
| 158 | if resp[0] <> '2': |
| 159 | raise error_reply, resp |
| 160 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 161 | # Send a PORT command with the current host and the given port number |
| 162 | def sendport(self, port): |
| 163 | hostname = socket.gethostname() |
| 164 | hostaddr = socket.gethostbyname(hostname) |
| 165 | hbytes = string.splitfields(hostaddr, '.') |
| 166 | pbytes = [`port/256`, `port%256`] |
| 167 | bytes = hbytes + pbytes |
| 168 | cmd = 'PORT ' + string.joinfields(bytes, ',') |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 169 | self.voidcmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 170 | |
| 171 | # Create a new socket and send a PORT command for it |
| 172 | def makeport(self): |
| 173 | global nextport |
| 174 | port = nextport + PORT_OFFSET |
| 175 | nextport = (nextport + 1) % PORT_CYCLE |
| 176 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 177 | sock.bind('', port) |
| 178 | sock.listen(0) |
| 179 | resp = self.sendport(port) |
| 180 | return sock |
| 181 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 182 | # Send a port command and a transfer command, accept the connection |
| 183 | # and return the socket for the connection |
| 184 | def transfercmd(self, cmd): |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 185 | sock = self.makeport() |
| 186 | resp = self.sendcmd(cmd) |
| 187 | if resp[0] <> '1': |
| 188 | raise error_reply, resp |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 189 | conn, sockaddr = sock.accept() |
| 190 | return conn |
| 191 | |
| 192 | # Login, default anonymous |
| 193 | def login(self, *args): |
| 194 | user = passwd = acct = '' |
| 195 | n = len(args) |
| 196 | if n > 3: raise TypeError, 'too many arguments' |
| 197 | if n > 0: user = args[0] |
| 198 | if n > 1: passwd = args[1] |
| 199 | if n > 2: acct = args[2] |
| 200 | if not user: user = 'anonymous' |
| 201 | if user == 'anonymous' and passwd in ('', '-'): |
| 202 | thishost = socket.gethostname() |
| 203 | if os.environ.has_key('LOGNAME'): |
| 204 | realuser = os.environ['LOGNAME'] |
| 205 | elif os.environ.has_key('USER'): |
| 206 | realuser = os.environ['USER'] |
| 207 | else: |
| 208 | realuser = 'anonymous' |
| 209 | passwd = passwd + realuser + '@' + thishost |
| 210 | resp = self.sendcmd('USER ' + user) |
| 211 | if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd) |
| 212 | if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct) |
| 213 | if resp[0] <> '2': |
| 214 | raise error_reply, resp |
| 215 | |
| 216 | # Retrieve data in binary mode. |
| 217 | # The argument is a RETR command. |
| 218 | # The callback function is called for each block. |
| 219 | # This creates a new port for you |
| 220 | def retrbinary(self, cmd, callback, blocksize): |
| 221 | self.voidcmd('TYPE I') |
| 222 | conn = self.transfercmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 223 | while 1: |
| 224 | data = conn.recv(blocksize) |
| 225 | if not data: |
| 226 | break |
| 227 | callback(data) |
| 228 | conn.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 229 | self.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 231 | # Retrieve data in line mode. |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 232 | # The argument is a RETR or LIST command. |
| 233 | # The callback function is called for each line, with trailing |
| 234 | # CRLF stripped. This creates a new port for you |
| 235 | def retrlines(self, cmd, callback): |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 236 | resp = self.sendcmd('TYPE A') |
| 237 | conn = self.transfercmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 238 | fp = conn.makefile('r') |
| 239 | while 1: |
| 240 | line = fp.readline() |
| 241 | if not line: |
| 242 | break |
| 243 | if line[-2:] == CRLF: |
| 244 | line = line[:-2] |
| 245 | elif line[:-1] == '\n': |
| 246 | line = line[:-1] |
| 247 | callback(line) |
| 248 | fp.close() |
| 249 | conn.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 250 | self.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 252 | # Store a file in binary mode |
| 253 | def storbinary(self, cmd, fp, blocksize): |
| 254 | self.voidcmd('TYPE I') |
| 255 | conn = self.transfercmd(cmd) |
| 256 | while 1: |
| 257 | buf = fp.read(blocksize) |
| 258 | if not buf: break |
| 259 | conn.send(buf) |
| 260 | conn.close() |
| 261 | self.voidresp() |
| 262 | |
| 263 | # Store a file in line mode |
| 264 | def storlines(self, cmd, fp): |
| 265 | self.voidcmd('TYPE A') |
| 266 | conn = self.transfercmd(cmd) |
| 267 | while 1: |
| 268 | buf = fp.readline() |
| 269 | if not buf: break |
| 270 | if buf[-2:] <> CRLF: |
| 271 | if buf[-1] in CRLF: buf = buf[:-1] |
| 272 | buf = buf + CRLF |
| 273 | conn.send(buf) |
| 274 | conn.close() |
| 275 | self.voidresp() |
| 276 | |
| 277 | # Return a list of files in a given directory (default the current) |
| 278 | def nlst(self, *args): |
| 279 | cmd = 'NLST' |
| 280 | for arg in args: |
| 281 | cmd = cmd + (' ' + arg) |
| 282 | files = [] |
| 283 | self.retrlines(cmd, files.append) |
| 284 | return files |
| 285 | |
| 286 | # Rename a file |
| 287 | def rename(self, fromname, toname): |
| 288 | resp = self.sendcmd('RNFR ' + fromname) |
| 289 | if resp[0] <> '3': |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 290 | raise error_reply, resp |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 291 | self.voidcmd('RNTO ' + toname) |
| 292 | |
| 293 | # Make a directory, return its full pathname |
| 294 | def mkd(self, dirname): |
| 295 | resp = self.sendcmd('MKD ' + dirname) |
| 296 | return parse257(resp) |
| 297 | |
| 298 | # Return current wording directory |
| 299 | def pwd(self): |
| 300 | resp = self.sendcmd('PWD') |
| 301 | return parse257(resp) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 302 | |
| 303 | # Quit, and close the connection |
| 304 | def quit(self): |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 305 | self.voidcmd('QUIT') |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 306 | self.file.close() |
| 307 | self.sock.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 308 | del self.file, self.sock |
| 309 | |
| 310 | |
| 311 | # Parse a response type 257 |
| 312 | def parse257(resp): |
| 313 | if resp[:3] <> '257': |
| 314 | raise error_reply, resp |
| 315 | if resp[3:5] <> ' "': |
| 316 | return '' # Not compliant to RFC 959, but UNIX ftpd does this |
| 317 | dirname = '' |
| 318 | i = 5 |
| 319 | n = len(resp) |
| 320 | while i < n: |
| 321 | c = resp[i] |
| 322 | i = i+1 |
| 323 | if c == '"': |
| 324 | if i >= n or resp[i] <> '"': |
| 325 | break |
| 326 | i = i+1 |
| 327 | dirname = dirname + c |
| 328 | return dirname |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 329 | |
| 330 | |
| 331 | # Test program. |
| 332 | # Usage: ftp [-d] host [-l[dir]] [-d[dir]] [file] ... |
| 333 | def test(): |
| 334 | import marshal |
| 335 | global nextport |
| 336 | try: |
| 337 | nextport = marshal.load(open('.@nextport', 'r')) |
| 338 | except IOError: |
| 339 | pass |
| 340 | try: |
| 341 | debugging = 0 |
| 342 | while sys.argv[1] == '-d': |
| 343 | debugging = debugging+1 |
| 344 | del sys.argv[1] |
| 345 | host = sys.argv[1] |
| 346 | ftp = FTP().init(host) |
| 347 | ftp.debug(debugging) |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame^] | 348 | ftp.login() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 349 | def writeln(line): print line |
| 350 | for file in sys.argv[2:]: |
| 351 | if file[:2] == '-l': |
| 352 | cmd = 'LIST' |
| 353 | if file[2:]: cmd = cmd + ' ' + file[2:] |
| 354 | ftp.retrlines(cmd, writeln) |
| 355 | elif file[:2] == '-d': |
| 356 | cmd = 'CWD' |
| 357 | if file[2:]: cmd = cmd + ' ' + file[2:] |
| 358 | resp = ftp.sendcmd(cmd) |
| 359 | else: |
| 360 | ftp.retrbinary('RETR ' + file, \ |
| 361 | sys.stdout.write, 1024) |
| 362 | ftp.quit() |
| 363 | finally: |
| 364 | marshal.dump(nextport, open('.@nextport', 'w')) |