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