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 | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 35 | # Magic number from <socket.h> |
| 36 | MSG_OOB = 0x1 # Process data out of band |
| 37 | |
| 38 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 39 | # The standard FTP server control port |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 40 | FTP_PORT = 21 |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 41 | |
| 42 | |
Guido van Rossum | 2197479 | 1992-11-06 13:34:17 +0000 | [diff] [blame] | 43 | # Exception raised when an error or invalid response is received |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 44 | error_reply = 'ftplib.error_reply' # unexpected [123]xx reply |
| 45 | error_temp = 'ftplib.error_temp' # 4xx errors |
| 46 | error_perm = 'ftplib.error_perm' # 5xx errors |
| 47 | 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] | 48 | |
| 49 | |
Guido van Rossum | 2197479 | 1992-11-06 13:34:17 +0000 | [diff] [blame] | 50 | # All exceptions (hopefully) that may be raised here and that aren't |
| 51 | # (always) programming errors on our side |
| 52 | all_errors = (error_reply, error_temp, error_perm, error_proto, \ |
| 53 | socket.error, IOError) |
| 54 | |
| 55 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 56 | # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) |
| 57 | CRLF = '\r\n' |
| 58 | |
| 59 | |
| 60 | # Next port to be used by makeport(), with PORT_OFFSET added |
Guido van Rossum | c68a401 | 1992-11-05 23:01:42 +0000 | [diff] [blame] | 61 | # (This is now only used when the python interpreter doesn't support |
| 62 | # the getsockname() method yet) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 63 | nextport = 0 |
| 64 | PORT_OFFSET = 40000 |
| 65 | PORT_CYCLE = 1000 |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | # The class itself |
| 69 | class FTP: |
| 70 | |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 71 | # New initialization method (called by class instantiation) |
| 72 | # Initialize host to localhost, port to standard ftp port |
| 73 | def __init__(self, *args): |
| 74 | # Initialize the instance to something mostly harmless |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 75 | self.debugging = 0 |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 76 | self.host = '' |
| 77 | self.port = FTP_PORT |
| 78 | self.sock = None |
| 79 | self.file = None |
| 80 | self.welcome = None |
| 81 | if args: |
| 82 | apply(self.connect, args) |
| 83 | |
| 84 | # Old init method (explicitly called by caller) |
| 85 | def init(self, *args): |
| 86 | if args: |
| 87 | apply(self.connect, args) |
| 88 | |
| 89 | # Connect to host. Arguments: |
| 90 | # - host: hostname to connect to (default previous host) |
| 91 | # - port: port to connect to (default previous port) |
| 92 | def init(self, *args): |
| 93 | if args: self.host = args[0] |
| 94 | if args[1:]: self.port = args[1] |
| 95 | if args[2:]: raise TypeError, 'too many args' |
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() |
| 100 | return self |
| 101 | |
| 102 | # Get the welcome message from the server |
| 103 | # (this is read and squirreled away by init()) |
| 104 | def getwelcome(self): |
| 105 | if self.debugging: print '*welcome*', `self.welcome` |
| 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 |
| 112 | def debug(self, level): |
| 113 | self.debugging = level |
| 114 | |
| 115 | # Internal: send one line to the server, appending CRLF |
| 116 | def putline(self, line): |
| 117 | line = line + CRLF |
| 118 | if self.debugging > 1: print '*put*', `line` |
| 119 | self.sock.send(line) |
| 120 | |
| 121 | # Internal: send one command to the server (through putline()) |
| 122 | def putcmd(self, line): |
| 123 | if self.debugging: print '*cmd*', `line` |
| 124 | self.putline(line) |
| 125 | |
| 126 | # Internal: return one line from the server, stripping CRLF. |
| 127 | # Raise EOFError if the connection is closed |
| 128 | def getline(self): |
| 129 | line = self.file.readline() |
| 130 | if self.debugging > 1: |
| 131 | print '*get*', `line` |
| 132 | if not line: raise EOFError |
| 133 | if line[-2:] == CRLF: line = line[:-2] |
| 134 | elif line[-1:] in CRLF: line = line[:-1] |
| 135 | return line |
| 136 | |
| 137 | # Internal: get a response from the server, which may possibly |
| 138 | # consist of multiple lines. Return a single string with no |
| 139 | # trailing CRLF. If the response consists of multiple lines, |
| 140 | # these are separated by '\n' characters in the string |
| 141 | def getmultiline(self): |
| 142 | line = self.getline() |
| 143 | if line[3:4] == '-': |
| 144 | code = line[:3] |
| 145 | while 1: |
| 146 | nextline = self.getline() |
| 147 | line = line + ('\n' + nextline) |
| 148 | if nextline[:3] == code and \ |
| 149 | nextline[3:4] <> '-': |
| 150 | break |
| 151 | return line |
| 152 | |
| 153 | # Internal: get a response from the server. |
| 154 | # Raise various errors if the response indicates an error |
| 155 | def getresp(self): |
| 156 | resp = self.getmultiline() |
| 157 | if self.debugging: print '*resp*', `resp` |
| 158 | self.lastresp = resp[:3] |
| 159 | c = resp[:1] |
| 160 | if c == '4': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 161 | raise error_temp, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 162 | if c == '5': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 163 | raise error_perm, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 164 | if c not in '123': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 165 | raise error_proto, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 166 | return resp |
| 167 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 168 | # Expect a response beginning with '2' |
| 169 | def voidresp(self): |
| 170 | resp = self.getresp() |
| 171 | if resp[0] <> '2': |
| 172 | raise error_reply, resp |
| 173 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 174 | # Abort a file transfer. Uses out-of-band data. |
| 175 | # This does not follow the procedure from the RFC to send Telnet |
| 176 | # IP and Synch; that doesn't seem to work with the servers I've |
| 177 | # tried. Instead, just send the ABOR command as OOB data. |
| 178 | def abort(self): |
| 179 | line = 'ABOR' + CRLF |
| 180 | if self.debugging > 1: print '*put urgent*', `line` |
| 181 | self.sock.send(line, MSG_OOB) |
| 182 | resp = self.getmultiline() |
| 183 | if resp[:3] not in ('426', '226'): |
| 184 | raise error_proto, resp |
| 185 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 186 | # Send a command and return the response |
| 187 | def sendcmd(self, cmd): |
| 188 | self.putcmd(cmd) |
| 189 | return self.getresp() |
| 190 | |
Guido van Rossum | c68a401 | 1992-11-05 23:01:42 +0000 | [diff] [blame] | 191 | # Send a command and expect a response beginning with '2' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 192 | def voidcmd(self, cmd): |
Guido van Rossum | c68a401 | 1992-11-05 23:01:42 +0000 | [diff] [blame] | 193 | self.putcmd(cmd) |
| 194 | self.voidresp() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 196 | # Send a PORT command with the current host and the given port number |
| 197 | def sendport(self, port): |
| 198 | hostname = socket.gethostname() |
| 199 | hostaddr = socket.gethostbyname(hostname) |
| 200 | hbytes = string.splitfields(hostaddr, '.') |
| 201 | pbytes = [`port/256`, `port%256`] |
| 202 | bytes = hbytes + pbytes |
| 203 | cmd = 'PORT ' + string.joinfields(bytes, ',') |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 204 | self.voidcmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 205 | |
| 206 | # Create a new socket and send a PORT command for it |
| 207 | def makeport(self): |
| 208 | global nextport |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 209 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | c68a401 | 1992-11-05 23:01:42 +0000 | [diff] [blame] | 210 | try: |
| 211 | getsockname = sock.getsockname |
| 212 | except AttributeError: |
| 213 | if self.debugging > 1: |
| 214 | print '*** getsockname not supported', |
| 215 | print '-- using manual port assignment ***' |
| 216 | port = nextport + PORT_OFFSET |
| 217 | nextport = (nextport + 1) % PORT_CYCLE |
| 218 | sock.bind('', port) |
| 219 | getsockname = None |
| 220 | sock.listen(0) # Assigns the port if not explicitly bound |
| 221 | if getsockname: |
| 222 | host, port = getsockname() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 223 | resp = self.sendport(port) |
| 224 | return sock |
| 225 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 226 | # Send a port command and a transfer command, accept the connection |
| 227 | # and return the socket for the connection |
| 228 | def transfercmd(self, cmd): |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 229 | sock = self.makeport() |
| 230 | resp = self.sendcmd(cmd) |
| 231 | if resp[0] <> '1': |
| 232 | raise error_reply, resp |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 233 | conn, sockaddr = sock.accept() |
| 234 | return conn |
| 235 | |
| 236 | # Login, default anonymous |
| 237 | def login(self, *args): |
| 238 | user = passwd = acct = '' |
| 239 | n = len(args) |
| 240 | if n > 3: raise TypeError, 'too many arguments' |
| 241 | if n > 0: user = args[0] |
| 242 | if n > 1: passwd = args[1] |
| 243 | if n > 2: acct = args[2] |
| 244 | if not user: user = 'anonymous' |
| 245 | if user == 'anonymous' and passwd in ('', '-'): |
| 246 | thishost = socket.gethostname() |
| 247 | if os.environ.has_key('LOGNAME'): |
| 248 | realuser = os.environ['LOGNAME'] |
| 249 | elif os.environ.has_key('USER'): |
| 250 | realuser = os.environ['USER'] |
| 251 | else: |
| 252 | realuser = 'anonymous' |
| 253 | passwd = passwd + realuser + '@' + thishost |
| 254 | resp = self.sendcmd('USER ' + user) |
| 255 | if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd) |
| 256 | if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct) |
| 257 | if resp[0] <> '2': |
| 258 | raise error_reply, resp |
| 259 | |
| 260 | # Retrieve data in binary mode. |
| 261 | # The argument is a RETR command. |
| 262 | # The callback function is called for each block. |
| 263 | # This creates a new port for you |
| 264 | def retrbinary(self, cmd, callback, blocksize): |
| 265 | self.voidcmd('TYPE I') |
| 266 | conn = self.transfercmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 267 | while 1: |
| 268 | data = conn.recv(blocksize) |
| 269 | if not data: |
| 270 | break |
| 271 | callback(data) |
| 272 | conn.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 273 | self.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 275 | # Retrieve data in line mode. |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 276 | # The argument is a RETR or LIST command. |
| 277 | # The callback function is called for each line, with trailing |
| 278 | # CRLF stripped. This creates a new port for you |
| 279 | def retrlines(self, cmd, callback): |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 280 | resp = self.sendcmd('TYPE A') |
| 281 | conn = self.transfercmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 282 | fp = conn.makefile('r') |
| 283 | while 1: |
| 284 | line = fp.readline() |
| 285 | if not line: |
| 286 | break |
| 287 | if line[-2:] == CRLF: |
| 288 | line = line[:-2] |
| 289 | elif line[:-1] == '\n': |
| 290 | line = line[:-1] |
| 291 | callback(line) |
| 292 | fp.close() |
| 293 | conn.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 294 | self.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 296 | # Store a file in binary mode |
| 297 | def storbinary(self, cmd, fp, blocksize): |
| 298 | self.voidcmd('TYPE I') |
| 299 | conn = self.transfercmd(cmd) |
| 300 | while 1: |
| 301 | buf = fp.read(blocksize) |
| 302 | if not buf: break |
| 303 | conn.send(buf) |
| 304 | conn.close() |
| 305 | self.voidresp() |
| 306 | |
| 307 | # Store a file in line mode |
| 308 | def storlines(self, cmd, fp): |
| 309 | self.voidcmd('TYPE A') |
| 310 | conn = self.transfercmd(cmd) |
| 311 | while 1: |
| 312 | buf = fp.readline() |
| 313 | if not buf: break |
| 314 | if buf[-2:] <> CRLF: |
| 315 | if buf[-1] in CRLF: buf = buf[:-1] |
| 316 | buf = buf + CRLF |
| 317 | conn.send(buf) |
| 318 | conn.close() |
| 319 | self.voidresp() |
| 320 | |
| 321 | # Return a list of files in a given directory (default the current) |
| 322 | def nlst(self, *args): |
| 323 | cmd = 'NLST' |
| 324 | for arg in args: |
| 325 | cmd = cmd + (' ' + arg) |
| 326 | files = [] |
| 327 | self.retrlines(cmd, files.append) |
| 328 | return files |
| 329 | |
| 330 | # Rename a file |
| 331 | def rename(self, fromname, toname): |
| 332 | resp = self.sendcmd('RNFR ' + fromname) |
| 333 | if resp[0] <> '3': |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 334 | raise error_reply, resp |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 335 | self.voidcmd('RNTO ' + toname) |
| 336 | |
Guido van Rossum | 02cf582 | 1993-05-17 08:00:02 +0000 | [diff] [blame] | 337 | # Change to a directory |
| 338 | def cwd(self, dirname): |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 339 | if dirname == '..': |
| 340 | cmd = 'CDUP' |
| 341 | else: |
| 342 | cmd = 'CWD ' + dirname |
| 343 | self.voidcmd(cmd) |
Guido van Rossum | 02cf582 | 1993-05-17 08:00:02 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 345 | # Retrieve the size of a file |
| 346 | def size(self, filename): |
| 347 | resp = self.sendcmd('SIZE ' + filename) |
| 348 | if resp[:3] == '213': |
| 349 | return string.atoi(string.strip(resp[3:])) |
| 350 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 351 | # Make a directory, return its full pathname |
| 352 | def mkd(self, dirname): |
| 353 | resp = self.sendcmd('MKD ' + dirname) |
| 354 | return parse257(resp) |
| 355 | |
| 356 | # Return current wording directory |
| 357 | def pwd(self): |
| 358 | resp = self.sendcmd('PWD') |
| 359 | return parse257(resp) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 360 | |
| 361 | # Quit, and close the connection |
| 362 | def quit(self): |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 363 | self.voidcmd('QUIT') |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 364 | self.close() |
| 365 | |
| 366 | # Close the connection without assuming anything about it |
| 367 | def close(self): |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 368 | self.file.close() |
| 369 | self.sock.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 370 | del self.file, self.sock |
| 371 | |
| 372 | |
| 373 | # Parse a response type 257 |
| 374 | def parse257(resp): |
| 375 | if resp[:3] <> '257': |
| 376 | raise error_reply, resp |
| 377 | if resp[3:5] <> ' "': |
| 378 | return '' # Not compliant to RFC 959, but UNIX ftpd does this |
| 379 | dirname = '' |
| 380 | i = 5 |
| 381 | n = len(resp) |
| 382 | while i < n: |
| 383 | c = resp[i] |
| 384 | i = i+1 |
| 385 | if c == '"': |
| 386 | if i >= n or resp[i] <> '"': |
| 387 | break |
| 388 | i = i+1 |
| 389 | dirname = dirname + c |
| 390 | return dirname |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 391 | |
| 392 | |
| 393 | # Test program. |
| 394 | # Usage: ftp [-d] host [-l[dir]] [-d[dir]] [file] ... |
| 395 | def test(): |
| 396 | import marshal |
| 397 | global nextport |
| 398 | try: |
| 399 | nextport = marshal.load(open('.@nextport', 'r')) |
| 400 | except IOError: |
| 401 | pass |
| 402 | try: |
| 403 | debugging = 0 |
| 404 | while sys.argv[1] == '-d': |
| 405 | debugging = debugging+1 |
| 406 | del sys.argv[1] |
| 407 | host = sys.argv[1] |
| 408 | ftp = FTP().init(host) |
| 409 | ftp.debug(debugging) |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 410 | ftp.login() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 411 | def writeln(line): print line |
| 412 | for file in sys.argv[2:]: |
| 413 | if file[:2] == '-l': |
| 414 | cmd = 'LIST' |
| 415 | if file[2:]: cmd = cmd + ' ' + file[2:] |
| 416 | ftp.retrlines(cmd, writeln) |
| 417 | elif file[:2] == '-d': |
| 418 | cmd = 'CWD' |
| 419 | if file[2:]: cmd = cmd + ' ' + file[2:] |
| 420 | resp = ftp.sendcmd(cmd) |
| 421 | else: |
| 422 | ftp.retrbinary('RETR ' + file, \ |
| 423 | sys.stdout.write, 1024) |
| 424 | ftp.quit() |
| 425 | finally: |
| 426 | marshal.dump(nextport, open('.@nextport', 'w')) |