Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 1 | '''An FTP client class, and some helper functions. |
| 2 | Based on RFC 959: File Transfer Protocol |
| 3 | (FTP), by J. Postel and J. Reynolds |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 5 | Changes and improvements suggested by Steve Majewski. |
| 6 | Modified by Jack to work on the mac. |
| 7 | Modified by Siebren to support docstrings and PASV. |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 10 | Example: |
| 11 | |
| 12 | >>> from ftplib import FTP |
| 13 | >>> ftp = FTP('ftp.python.org') # connect to host, default port |
| 14 | >>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 15 | '230 Guest login ok, access restrictions apply.' |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 16 | >>> ftp.retrlines('LIST') # list directory contents |
| 17 | total 9 |
| 18 | drwxr-xr-x 8 root wheel 1024 Jan 3 1994 . |
| 19 | drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .. |
| 20 | drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin |
| 21 | drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc |
| 22 | d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming |
| 23 | drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib |
| 24 | drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub |
| 25 | drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr |
| 26 | -rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 27 | '226 Transfer complete.' |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 28 | >>> ftp.quit() |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 29 | '221 Goodbye.' |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 30 | >>> |
| 31 | |
| 32 | A nice test that reveals some of the network dialogue would be: |
| 33 | python ftplib.py -d localhost -l -p -l |
| 34 | ''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 35 | |
| 36 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 37 | import os |
| 38 | import sys |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 39 | import string |
| 40 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 41 | # Import SOCKS module if it exists, else standard socket module socket |
| 42 | try: |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 43 | import SOCKS; socket = SOCKS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 44 | except ImportError: |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 45 | import socket |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 48 | # Magic number from <socket.h> |
| 49 | MSG_OOB = 0x1 # Process data out of band |
| 50 | |
| 51 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 52 | # The standard FTP server control port |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 53 | FTP_PORT = 21 |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 54 | |
| 55 | |
Guido van Rossum | 2197479 | 1992-11-06 13:34:17 +0000 | [diff] [blame] | 56 | # Exception raised when an error or invalid response is received |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 57 | error_reply = 'ftplib.error_reply' # unexpected [123]xx reply |
| 58 | error_temp = 'ftplib.error_temp' # 4xx errors |
| 59 | error_perm = 'ftplib.error_perm' # 5xx errors |
| 60 | 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] | 61 | |
| 62 | |
Guido van Rossum | 2197479 | 1992-11-06 13:34:17 +0000 | [diff] [blame] | 63 | # All exceptions (hopefully) that may be raised here and that aren't |
| 64 | # (always) programming errors on our side |
| 65 | all_errors = (error_reply, error_temp, error_perm, error_proto, \ |
Guido van Rossum | c0e68d1 | 1995-09-30 16:51:50 +0000 | [diff] [blame] | 66 | socket.error, IOError, EOFError) |
Guido van Rossum | 2197479 | 1992-11-06 13:34:17 +0000 | [diff] [blame] | 67 | |
| 68 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 69 | # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) |
| 70 | CRLF = '\r\n' |
| 71 | |
| 72 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 73 | # The class itself |
| 74 | class FTP: |
| 75 | |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 76 | '''An FTP client class. |
| 77 | |
| 78 | To create a connection, call the class using these argument: |
| 79 | host, user, passwd, acct |
| 80 | These are all strings, and have default value ''. |
| 81 | Then use self.connect() with optional host and port argument. |
| 82 | |
| 83 | To download a file, use ftp.retrlines('RETR ' + filename), |
| 84 | or ftp.retrbinary() with slightly different arguments. |
| 85 | To upload a file, use ftp.storlines() or ftp.storbinary(), |
| 86 | which have an open file as argument (see their definitions |
| 87 | below for details). |
| 88 | The download/upload functions first issue appropriate TYPE |
| 89 | and PORT or PASV commands. |
| 90 | ''' |
| 91 | |
| 92 | # Initialization method (called by class instantiation). |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 93 | # Initialize host to localhost, port to standard ftp port |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 94 | # Optional arguments are host (for connect()), |
| 95 | # and user, passwd, acct (for login()) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 96 | def __init__(self, host = '', user = '', passwd = '', acct = ''): |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 97 | # Initialize the instance to something mostly harmless |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 98 | self.debugging = 0 |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 99 | self.host = '' |
| 100 | self.port = FTP_PORT |
| 101 | self.sock = None |
| 102 | self.file = None |
| 103 | self.welcome = None |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 104 | resp = None |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 105 | if host: |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 106 | resp = self.connect(host) |
| 107 | if user: resp = self.login(user, passwd, acct) |
Guido van Rossum | 52fc1f6 | 1993-06-17 12:38:10 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 109 | def connect(self, host = '', port = 0): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 110 | '''Connect to host. Arguments are: |
| 111 | - host: hostname to connect to (string, default previous host) |
| 112 | - port: port to connect to (integer, default previous port)''' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 113 | if host: self.host = host |
| 114 | if port: self.port = port |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 115 | self.passiveserver = 0 |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 116 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 117 | self.sock.connect(self.host, self.port) |
Guido van Rossum | 24611f8 | 1996-09-30 22:02:50 +0000 | [diff] [blame] | 118 | self.file = self.sock.makefile('rb') |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 119 | self.welcome = self.getresp() |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 120 | return self.welcome |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 122 | def getwelcome(self): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 123 | '''Get the welcome message from the server. |
| 124 | (this is read and squirreled away by connect())''' |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 125 | if self.debugging: |
| 126 | print '*welcome*', self.sanitize(self.welcome) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 127 | return self.welcome |
| 128 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 129 | def set_debuglevel(self, level): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 130 | '''Set the debugging level. |
| 131 | The required argument level means: |
| 132 | 0: no debugging output (default) |
| 133 | 1: print commands and responses but not body text etc. |
| 134 | 2: also print raw lines read and sent before stripping CR/LF''' |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 135 | self.debugging = level |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 136 | debug = set_debuglevel |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 138 | def set_pasv(self, val): |
| 139 | '''Use passive or active mode for data transfers. |
| 140 | With a false argument, use the normal PORT mode, |
| 141 | With a true argument, use the PASV command.''' |
| 142 | self.passiveserver = val |
| 143 | |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 144 | # Internal: "sanitize" a string for printing |
| 145 | def sanitize(self, s): |
| 146 | if s[:5] == 'pass ' or s[:5] == 'PASS ': |
| 147 | i = len(s) |
| 148 | while i > 5 and s[i-1] in '\r\n': |
| 149 | i = i-1 |
| 150 | s = s[:5] + '*'*(i-5) + s[i:] |
| 151 | return `s` |
| 152 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 153 | # Internal: send one line to the server, appending CRLF |
| 154 | def putline(self, line): |
| 155 | line = line + CRLF |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 156 | if self.debugging > 1: print '*put*', self.sanitize(line) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 157 | self.sock.send(line) |
| 158 | |
| 159 | # Internal: send one command to the server (through putline()) |
| 160 | def putcmd(self, line): |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 161 | if self.debugging: print '*cmd*', self.sanitize(line) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 162 | self.putline(line) |
| 163 | |
| 164 | # Internal: return one line from the server, stripping CRLF. |
| 165 | # Raise EOFError if the connection is closed |
| 166 | def getline(self): |
| 167 | line = self.file.readline() |
| 168 | if self.debugging > 1: |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 169 | print '*get*', self.sanitize(line) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 170 | if not line: raise EOFError |
| 171 | if line[-2:] == CRLF: line = line[:-2] |
| 172 | elif line[-1:] in CRLF: line = line[:-1] |
| 173 | return line |
| 174 | |
| 175 | # Internal: get a response from the server, which may possibly |
| 176 | # consist of multiple lines. Return a single string with no |
| 177 | # trailing CRLF. If the response consists of multiple lines, |
| 178 | # these are separated by '\n' characters in the string |
| 179 | def getmultiline(self): |
| 180 | line = self.getline() |
| 181 | if line[3:4] == '-': |
| 182 | code = line[:3] |
| 183 | while 1: |
| 184 | nextline = self.getline() |
| 185 | line = line + ('\n' + nextline) |
| 186 | if nextline[:3] == code and \ |
| 187 | nextline[3:4] <> '-': |
| 188 | break |
| 189 | return line |
| 190 | |
| 191 | # Internal: get a response from the server. |
| 192 | # Raise various errors if the response indicates an error |
| 193 | def getresp(self): |
| 194 | resp = self.getmultiline() |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 195 | if self.debugging: print '*resp*', self.sanitize(resp) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 196 | self.lastresp = resp[:3] |
| 197 | c = resp[:1] |
| 198 | if c == '4': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 199 | raise error_temp, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 200 | if c == '5': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 201 | raise error_perm, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 202 | if c not in '123': |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 203 | raise error_proto, resp |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 204 | return resp |
| 205 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 206 | def voidresp(self): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 207 | """Expect a response beginning with '2'.""" |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 208 | resp = self.getresp() |
| 209 | if resp[0] <> '2': |
| 210 | raise error_reply, resp |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 211 | return resp |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 213 | def abort(self): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 214 | '''Abort a file transfer. Uses out-of-band data. |
| 215 | This does not follow the procedure from the RFC to send Telnet |
| 216 | IP and Synch; that doesn't seem to work with the servers I've |
| 217 | tried. Instead, just send the ABOR command as OOB data.''' |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 218 | line = 'ABOR' + CRLF |
Guido van Rossum | ebaf104 | 1995-05-05 15:54:14 +0000 | [diff] [blame] | 219 | if self.debugging > 1: print '*put urgent*', self.sanitize(line) |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 220 | self.sock.send(line, MSG_OOB) |
| 221 | resp = self.getmultiline() |
| 222 | if resp[:3] not in ('426', '226'): |
| 223 | raise error_proto, resp |
| 224 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 225 | def sendcmd(self, cmd): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 226 | '''Send a command and return the response.''' |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 227 | self.putcmd(cmd) |
| 228 | return self.getresp() |
| 229 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 230 | def voidcmd(self, cmd): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 231 | """Send a command and expect a response beginning with '2'.""" |
Guido van Rossum | c68a401 | 1992-11-05 23:01:42 +0000 | [diff] [blame] | 232 | self.putcmd(cmd) |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 233 | return self.voidresp() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | 221ec0b | 1995-08-04 04:39:30 +0000 | [diff] [blame] | 235 | def sendport(self, host, port): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 236 | '''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] | 237 | hbytes = string.splitfields(host, '.') |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 238 | pbytes = [`port/256`, `port%256`] |
| 239 | bytes = hbytes + pbytes |
| 240 | cmd = 'PORT ' + string.joinfields(bytes, ',') |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 241 | return self.voidcmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 243 | def makeport(self): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 244 | '''Create a new socket and send a PORT command for it.''' |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 245 | global nextport |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 246 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | 303c179 | 1995-06-20 17:21:42 +0000 | [diff] [blame] | 247 | sock.bind(('', 0)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | sock.listen(1) |
Guido van Rossum | 221ec0b | 1995-08-04 04:39:30 +0000 | [diff] [blame] | 249 | dummyhost, port = sock.getsockname() # Get proper port |
| 250 | host, dummyport = self.sock.getsockname() # Get proper host |
| 251 | resp = self.sendport(host, port) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 252 | return sock |
| 253 | |
Fred Drake | 4de02d9 | 1997-01-10 18:26:09 +0000 | [diff] [blame] | 254 | def ntransfercmd(self, cmd): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 255 | '''Initiate a transfer over the data connection. |
| 256 | If the transfer is active, send a port command and |
| 257 | the transfer command, and accept the connection. |
| 258 | If the server is passive, send a pasv command, connect |
| 259 | to it, and start the transfer command. |
Fred Drake | 4de02d9 | 1997-01-10 18:26:09 +0000 | [diff] [blame] | 260 | Either way, return the socket for the connection and |
| 261 | the expected size of the transfer. The expected size |
| 262 | may be None if it could not be determined.''' |
| 263 | size = None |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 264 | if self.passiveserver: |
| 265 | host, port = parse227(self.sendcmd('PASV')) |
| 266 | conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 267 | conn.connect(host, port) |
| 268 | resp = self.sendcmd(cmd) |
| 269 | if resp[0] <> '1': |
| 270 | raise error_reply, resp |
| 271 | else: |
| 272 | sock = self.makeport() |
| 273 | resp = self.sendcmd(cmd) |
| 274 | if resp[0] <> '1': |
| 275 | raise error_reply, resp |
| 276 | conn, sockaddr = sock.accept() |
Fred Drake | 4de02d9 | 1997-01-10 18:26:09 +0000 | [diff] [blame] | 277 | if resp[:3] == '150': |
| 278 | # this is conditional in case we received a 125 |
| 279 | size = parse150(resp) |
| 280 | return conn, size |
| 281 | |
| 282 | def transfercmd(self, cmd): |
| 283 | '''Initiate a transfer over the data connection. Returns |
| 284 | the socket for the connection. See also ntransfercmd().''' |
| 285 | return self.ntransfercmd(cmd)[0] |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | def login(self, user = '', passwd = '', acct = ''): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 288 | '''Login, default anonymous.''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 289 | if not user: user = 'anonymous' |
Guido van Rossum | 9824509 | 1998-02-19 21:15:44 +0000 | [diff] [blame] | 290 | if not passwd: passwd = '' |
| 291 | if not acct: acct = '' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 292 | if user == 'anonymous' and passwd in ('', '-'): |
| 293 | thishost = socket.gethostname() |
Jack Jansen | 2db6bfc | 1995-05-04 15:02:18 +0000 | [diff] [blame] | 294 | # Make sure it is fully qualified |
| 295 | if not '.' in thishost: |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 296 | thisaddr = socket.gethostbyname(thishost) |
| 297 | firstname, names, unused = \ |
| 298 | socket.gethostbyaddr(thisaddr) |
| 299 | names.insert(0, firstname) |
| 300 | for name in names: |
| 301 | if '.' in name: |
| 302 | thishost = name |
| 303 | break |
Jack Jansen | 40b9835 | 1995-01-19 12:24:45 +0000 | [diff] [blame] | 304 | try: |
| 305 | if os.environ.has_key('LOGNAME'): |
| 306 | realuser = os.environ['LOGNAME'] |
| 307 | elif os.environ.has_key('USER'): |
| 308 | realuser = os.environ['USER'] |
| 309 | else: |
| 310 | realuser = 'anonymous' |
| 311 | except AttributeError: |
| 312 | # Not all systems have os.environ.... |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 313 | realuser = 'anonymous' |
| 314 | passwd = passwd + realuser + '@' + thishost |
| 315 | resp = self.sendcmd('USER ' + user) |
| 316 | if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd) |
| 317 | if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct) |
| 318 | if resp[0] <> '2': |
| 319 | raise error_reply, resp |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 320 | return resp |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | ab76af3 | 1997-12-03 19:34:14 +0000 | [diff] [blame] | 322 | def retrbinary(self, cmd, callback, blocksize=8192): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 323 | '''Retrieve data in binary mode. |
| 324 | The argument is a RETR command. |
| 325 | The callback function is called for each block. |
| 326 | This creates a new port for you''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 327 | self.voidcmd('TYPE I') |
| 328 | conn = self.transfercmd(cmd) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 329 | while 1: |
| 330 | data = conn.recv(blocksize) |
| 331 | if not data: |
| 332 | break |
| 333 | callback(data) |
| 334 | conn.close() |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 335 | return self.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 337 | def retrlines(self, cmd, callback = None): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 338 | '''Retrieve data in line mode. |
| 339 | The argument is a RETR or LIST command. |
| 340 | The callback function (2nd argument) is called for each line, |
| 341 | with trailing CRLF stripped. This creates a new port for you. |
Fred Drake | d5f173b | 1999-07-07 13:36:59 +0000 | [diff] [blame] | 342 | print_line() is the default callback.''' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 343 | if not callback: callback = print_line |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 344 | resp = self.sendcmd('TYPE A') |
| 345 | conn = self.transfercmd(cmd) |
Guido van Rossum | 24611f8 | 1996-09-30 22:02:50 +0000 | [diff] [blame] | 346 | fp = conn.makefile('rb') |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 347 | while 1: |
| 348 | line = fp.readline() |
Guido van Rossum | c0e68d1 | 1995-09-30 16:51:50 +0000 | [diff] [blame] | 349 | if self.debugging > 2: print '*retr*', `line` |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 350 | if not line: |
| 351 | break |
| 352 | if line[-2:] == CRLF: |
| 353 | line = line[:-2] |
Guido van Rossum | c6769c5 | 1998-12-21 16:26:31 +0000 | [diff] [blame] | 354 | elif line[-1:] == '\n': |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 355 | line = line[:-1] |
| 356 | callback(line) |
| 357 | fp.close() |
| 358 | conn.close() |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 359 | return self.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 361 | def storbinary(self, cmd, fp, blocksize): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 362 | '''Store a file in binary mode.''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 363 | self.voidcmd('TYPE I') |
| 364 | conn = self.transfercmd(cmd) |
| 365 | while 1: |
| 366 | buf = fp.read(blocksize) |
| 367 | if not buf: break |
| 368 | conn.send(buf) |
| 369 | conn.close() |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 370 | return self.voidresp() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 371 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 372 | def storlines(self, cmd, fp): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 373 | '''Store a file in line mode.''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 374 | self.voidcmd('TYPE A') |
| 375 | conn = self.transfercmd(cmd) |
| 376 | while 1: |
| 377 | buf = fp.readline() |
| 378 | if not buf: break |
| 379 | if buf[-2:] <> CRLF: |
| 380 | if buf[-1] in CRLF: buf = buf[:-1] |
| 381 | buf = buf + CRLF |
| 382 | conn.send(buf) |
| 383 | conn.close() |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 384 | return self.voidresp() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | 0eaa74b | 1996-01-25 18:37:21 +0000 | [diff] [blame] | 386 | def acct(self, password): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 387 | '''Send new account name.''' |
Guido van Rossum | 0eaa74b | 1996-01-25 18:37:21 +0000 | [diff] [blame] | 388 | cmd = 'ACCT ' + password |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 389 | return self.voidcmd(cmd) |
Guido van Rossum | 0eaa74b | 1996-01-25 18:37:21 +0000 | [diff] [blame] | 390 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 391 | def nlst(self, *args): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 392 | '''Return a list of files in a given directory (default the current).''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 393 | cmd = 'NLST' |
| 394 | for arg in args: |
| 395 | cmd = cmd + (' ' + arg) |
| 396 | files = [] |
| 397 | self.retrlines(cmd, files.append) |
| 398 | return files |
| 399 | |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 400 | def dir(self, *args): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 401 | '''List a directory in long form. |
| 402 | By default list current directory to stdout. |
| 403 | Optional last argument is callback function; all |
| 404 | non-empty arguments before it are concatenated to the |
| 405 | LIST command. (This *should* only be used for a pathname.)''' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 406 | cmd = 'LIST' |
| 407 | func = None |
| 408 | if args[-1:] and type(args[-1]) != type(''): |
| 409 | args, func = args[:-1], args[-1] |
| 410 | for arg in args: |
| 411 | if arg: |
| 412 | cmd = cmd + (' ' + arg) |
| 413 | self.retrlines(cmd, func) |
| 414 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 415 | def rename(self, fromname, toname): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 416 | '''Rename a file.''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 417 | resp = self.sendcmd('RNFR ' + fromname) |
| 418 | if resp[0] <> '3': |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 419 | raise error_reply, resp |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 420 | return self.voidcmd('RNTO ' + toname) |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 421 | |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 422 | def delete(self, filename): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 423 | '''Delete a file.''' |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 424 | resp = self.sendcmd('DELE ' + filename) |
Guido van Rossum | 6bbd1d0 | 1998-07-02 20:41:20 +0000 | [diff] [blame] | 425 | if resp[:3] in ('250', '200'): |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 426 | return resp |
| 427 | elif resp[:1] == '5': |
| 428 | raise error_perm, resp |
| 429 | else: |
| 430 | raise error_reply, resp |
Guido van Rossum | a61bdeb | 1995-10-11 17:36:31 +0000 | [diff] [blame] | 431 | |
Guido van Rossum | 02cf582 | 1993-05-17 08:00:02 +0000 | [diff] [blame] | 432 | def cwd(self, dirname): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 433 | '''Change to a directory.''' |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 434 | if dirname == '..': |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 435 | try: |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 436 | return self.voidcmd('CDUP') |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 437 | except error_perm, msg: |
| 438 | if msg[:3] != '500': |
| 439 | raise error_perm, msg |
Guido van Rossum | 1ebcf6a | 1999-08-18 21:51:10 +0000 | [diff] [blame] | 440 | elif dirname == '': |
| 441 | dirname = '.' # does nothing, but could return error |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 442 | cmd = 'CWD ' + dirname |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 443 | return self.voidcmd(cmd) |
Guido van Rossum | 02cf582 | 1993-05-17 08:00:02 +0000 | [diff] [blame] | 444 | |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 445 | def size(self, filename): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 446 | '''Retrieve the size of a file.''' |
| 447 | # Note that the RFC doesn't say anything about 'SIZE' |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 448 | resp = self.sendcmd('SIZE ' + filename) |
| 449 | if resp[:3] == '213': |
| 450 | return string.atoi(string.strip(resp[3:])) |
| 451 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 452 | def mkd(self, dirname): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 453 | '''Make a directory, return its full pathname.''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 454 | resp = self.sendcmd('MKD ' + dirname) |
| 455 | return parse257(resp) |
| 456 | |
Guido van Rossum | 9824509 | 1998-02-19 21:15:44 +0000 | [diff] [blame] | 457 | def rmd(self, dirname): |
| 458 | '''Remove a directory.''' |
| 459 | return self.voidcmd('RMD ' + dirname) |
| 460 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 461 | def pwd(self): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 462 | '''Return current working directory.''' |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 463 | resp = self.sendcmd('PWD') |
| 464 | return parse257(resp) |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 465 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 466 | def quit(self): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 467 | '''Quit, and close the connection.''' |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 468 | resp = self.voidcmd('QUIT') |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 469 | self.close() |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 470 | return resp |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | 17ed1ae | 1993-06-01 13:21:04 +0000 | [diff] [blame] | 472 | def close(self): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 473 | '''Close the connection without assuming anything about it.''' |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 474 | self.file.close() |
| 475 | self.sock.close() |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 476 | del self.file, self.sock |
| 477 | |
| 478 | |
Guido van Rossum | acfb82a | 1997-10-22 20:49:52 +0000 | [diff] [blame] | 479 | _150_re = None |
Fred Drake | 4de02d9 | 1997-01-10 18:26:09 +0000 | [diff] [blame] | 480 | |
| 481 | def parse150(resp): |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 482 | '''Parse the '150' response for a RETR request. |
| 483 | Returns the expected transfer size or None; size is not guaranteed to |
| 484 | be present in the 150 message. |
| 485 | ''' |
| 486 | if resp[:3] != '150': |
| 487 | raise error_reply, resp |
| 488 | global _150_re |
| 489 | if _150_re is None: |
| 490 | import re |
Fred Drake | 9291d27 | 1998-04-27 14:39:44 +0000 | [diff] [blame] | 491 | _150_re = re.compile("150 .* \((\d+) bytes\)", re.IGNORECASE) |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 492 | m = _150_re.match(resp) |
| 493 | if m: |
| 494 | return string.atoi(m.group(1)) |
| 495 | return None |
Fred Drake | 4de02d9 | 1997-01-10 18:26:09 +0000 | [diff] [blame] | 496 | |
| 497 | |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 498 | def parse227(resp): |
| 499 | '''Parse the '227' response for a PASV request. |
| 500 | Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)' |
| 501 | Return ('host.addr.as.numbers', port#) tuple.''' |
| 502 | |
| 503 | if resp[:3] <> '227': |
| 504 | raise error_reply, resp |
| 505 | left = string.find(resp, '(') |
| 506 | if left < 0: raise error_proto, resp |
| 507 | right = string.find(resp, ')', left + 1) |
| 508 | if right < 0: |
| 509 | raise error_proto, resp # should contain '(h1,h2,h3,h4,p1,p2)' |
| 510 | numbers = string.split(resp[left+1:right], ',') |
| 511 | if len(numbers) <> 6: |
| 512 | raise error_proto, resp |
| 513 | host = string.join(numbers[:4], '.') |
| 514 | port = (string.atoi(numbers[4]) << 8) + string.atoi(numbers[5]) |
| 515 | return host, port |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 516 | |
| 517 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 518 | def parse257(resp): |
Guido van Rossum | 9824509 | 1998-02-19 21:15:44 +0000 | [diff] [blame] | 519 | '''Parse the '257' response for a MKD or PWD request. |
| 520 | This is a response to a MKD or PWD request: a directory name. |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 521 | Returns the directoryname in the 257 reply.''' |
| 522 | |
Guido van Rossum | c567c60 | 1992-11-05 22:22:37 +0000 | [diff] [blame] | 523 | if resp[:3] <> '257': |
| 524 | raise error_reply, resp |
| 525 | if resp[3:5] <> ' "': |
| 526 | return '' # Not compliant to RFC 959, but UNIX ftpd does this |
| 527 | dirname = '' |
| 528 | i = 5 |
| 529 | n = len(resp) |
| 530 | while i < n: |
| 531 | c = resp[i] |
| 532 | i = i+1 |
| 533 | if c == '"': |
| 534 | if i >= n or resp[i] <> '"': |
| 535 | break |
| 536 | i = i+1 |
| 537 | dirname = dirname + c |
| 538 | return dirname |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 539 | |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 540 | |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 541 | def print_line(line): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 542 | '''Default retrlines callback to print a line.''' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 543 | print line |
| 544 | |
Guido van Rossum | 2f3941d | 1997-10-07 14:49:56 +0000 | [diff] [blame] | 545 | |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 546 | def ftpcp(source, sourcename, target, targetname = '', type = 'I'): |
| 547 | '''Copy file from one FTP-instance to another.''' |
| 548 | if not targetname: targetname = sourcename |
| 549 | type = 'TYPE ' + type |
| 550 | source.voidcmd(type) |
| 551 | target.voidcmd(type) |
| 552 | sourcehost, sourceport = parse227(source.sendcmd('PASV')) |
| 553 | target.sendport(sourcehost, sourceport) |
| 554 | # RFC 959: the user must "listen" [...] BEFORE sending the |
| 555 | # transfer request. |
| 556 | # So: STOR before RETR, because here the target is a "user". |
| 557 | treply = target.sendcmd('STOR ' + targetname) |
| 558 | if treply[:3] not in ('125', '150'): raise error_proto # RFC 959 |
| 559 | sreply = source.sendcmd('RETR ' + sourcename) |
| 560 | if sreply[:3] not in ('125', '150'): raise error_proto # RFC 959 |
| 561 | source.voidresp() |
| 562 | target.voidresp() |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 56d1e3a | 1997-03-14 04:16:54 +0000 | [diff] [blame] | 564 | |
Guido van Rossum | 56d1e3a | 1997-03-14 04:16:54 +0000 | [diff] [blame] | 565 | class Netrc: |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 566 | """Class to parse & provide access to 'netrc' format files. |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 567 | |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 568 | See the netrc(4) man page for information on the file format. |
Guido van Rossum | 56d1e3a | 1997-03-14 04:16:54 +0000 | [diff] [blame] | 569 | |
Guido van Rossum | c822a45 | 1998-12-22 16:49:16 +0000 | [diff] [blame] | 570 | WARNING: This class is obsolete -- use module netrc instead. |
| 571 | |
Guido van Rossum | 56d1e3a | 1997-03-14 04:16:54 +0000 | [diff] [blame] | 572 | """ |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 573 | __defuser = None |
| 574 | __defpasswd = None |
| 575 | __defacct = None |
Guido van Rossum | 56d1e3a | 1997-03-14 04:16:54 +0000 | [diff] [blame] | 576 | |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 577 | def __init__(self, filename=None): |
| 578 | if not filename: |
| 579 | if os.environ.has_key("HOME"): |
| 580 | filename = os.path.join(os.environ["HOME"], |
| 581 | ".netrc") |
| 582 | else: |
| 583 | raise IOError, \ |
| 584 | "specify file to load or set $HOME" |
| 585 | self.__hosts = {} |
| 586 | self.__macros = {} |
| 587 | fp = open(filename, "r") |
| 588 | in_macro = 0 |
| 589 | while 1: |
| 590 | line = fp.readline() |
| 591 | if not line: break |
| 592 | if in_macro and string.strip(line): |
| 593 | macro_lines.append(line) |
| 594 | continue |
| 595 | elif in_macro: |
| 596 | self.__macros[macro_name] = tuple(macro_lines) |
| 597 | in_macro = 0 |
| 598 | words = string.split(line) |
| 599 | host = user = passwd = acct = None |
| 600 | default = 0 |
| 601 | i = 0 |
| 602 | while i < len(words): |
| 603 | w1 = words[i] |
| 604 | if i+1 < len(words): |
| 605 | w2 = words[i + 1] |
| 606 | else: |
| 607 | w2 = None |
| 608 | if w1 == 'default': |
| 609 | default = 1 |
| 610 | elif w1 == 'machine' and w2: |
| 611 | host = string.lower(w2) |
| 612 | i = i + 1 |
| 613 | elif w1 == 'login' and w2: |
| 614 | user = w2 |
| 615 | i = i + 1 |
| 616 | elif w1 == 'password' and w2: |
| 617 | passwd = w2 |
| 618 | i = i + 1 |
| 619 | elif w1 == 'account' and w2: |
| 620 | acct = w2 |
| 621 | i = i + 1 |
| 622 | elif w1 == 'macdef' and w2: |
| 623 | macro_name = w2 |
| 624 | macro_lines = [] |
| 625 | in_macro = 1 |
| 626 | break |
| 627 | i = i + 1 |
| 628 | if default: |
| 629 | self.__defuser = user or self.__defuser |
| 630 | self.__defpasswd = passwd or self.__defpasswd |
| 631 | self.__defacct = acct or self.__defacct |
| 632 | if host: |
| 633 | if self.__hosts.has_key(host): |
| 634 | ouser, opasswd, oacct = \ |
| 635 | self.__hosts[host] |
| 636 | user = user or ouser |
| 637 | passwd = passwd or opasswd |
| 638 | acct = acct or oacct |
| 639 | self.__hosts[host] = user, passwd, acct |
| 640 | fp.close() |
Guido van Rossum | 56d1e3a | 1997-03-14 04:16:54 +0000 | [diff] [blame] | 641 | |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 642 | def get_hosts(self): |
| 643 | """Return a list of hosts mentioned in the .netrc file.""" |
| 644 | return self.__hosts.keys() |
| 645 | |
| 646 | def get_account(self, host): |
| 647 | """Returns login information for the named host. |
| 648 | |
| 649 | The return value is a triple containing userid, |
| 650 | password, and the accounting field. |
| 651 | |
| 652 | """ |
| 653 | host = string.lower(host) |
| 654 | user = passwd = acct = None |
| 655 | if self.__hosts.has_key(host): |
| 656 | user, passwd, acct = self.__hosts[host] |
| 657 | user = user or self.__defuser |
| 658 | passwd = passwd or self.__defpasswd |
| 659 | acct = acct or self.__defacct |
| 660 | return user, passwd, acct |
| 661 | |
| 662 | def get_macros(self): |
| 663 | """Return a list of all defined macro names.""" |
| 664 | return self.__macros.keys() |
| 665 | |
| 666 | def get_macro(self, macro): |
| 667 | """Return a sequence of lines which define a named macro.""" |
| 668 | return self.__macros[macro] |
Guido van Rossum | 56d1e3a | 1997-03-14 04:16:54 +0000 | [diff] [blame] | 669 | |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 670 | |
Guido van Rossum | 56d1e3a | 1997-03-14 04:16:54 +0000 | [diff] [blame] | 671 | |
Guido van Rossum | 1115ab2 | 1992-11-04 15:51:30 +0000 | [diff] [blame] | 672 | def test(): |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 673 | '''Test program. |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 674 | Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...''' |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 675 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 676 | debugging = 0 |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 677 | rcfile = None |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 678 | while sys.argv[1] == '-d': |
| 679 | debugging = debugging+1 |
| 680 | del sys.argv[1] |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 681 | if sys.argv[1][:2] == '-r': |
| 682 | # get name of alternate ~/.netrc file: |
| 683 | rcfile = sys.argv[1][2:] |
| 684 | del sys.argv[1] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 685 | host = sys.argv[1] |
| 686 | ftp = FTP(host) |
| 687 | ftp.set_debuglevel(debugging) |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 688 | userid = passwd = acct = '' |
| 689 | try: |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 690 | netrc = Netrc(rcfile) |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 691 | except IOError: |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 692 | if rcfile is not None: |
| 693 | sys.stderr.write("Could not open account file" |
| 694 | " -- using anonymous login.") |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 695 | else: |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 696 | try: |
| 697 | userid, passwd, acct = netrc.get_account(host) |
| 698 | except KeyError: |
| 699 | # no account for host |
| 700 | sys.stderr.write( |
| 701 | "No account -- using anonymous login.") |
Fred Drake | 475d51d | 1997-06-24 22:02:54 +0000 | [diff] [blame] | 702 | ftp.login(userid, passwd, acct) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 703 | for file in sys.argv[2:]: |
| 704 | if file[:2] == '-l': |
| 705 | ftp.dir(file[2:]) |
| 706 | elif file[:2] == '-d': |
| 707 | cmd = 'CWD' |
| 708 | if file[2:]: cmd = cmd + ' ' + file[2:] |
| 709 | resp = ftp.sendcmd(cmd) |
Guido van Rossum | d2560b0 | 1996-05-28 23:41:25 +0000 | [diff] [blame] | 710 | elif file == '-p': |
| 711 | ftp.set_pasv(not ftp.passiveserver) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 712 | else: |
| 713 | ftp.retrbinary('RETR ' + file, \ |
| 714 | sys.stdout.write, 1024) |
| 715 | ftp.quit() |
Guido van Rossum | 221ec0b | 1995-08-04 04:39:30 +0000 | [diff] [blame] | 716 | |
| 717 | |
| 718 | if __name__ == '__main__': |
| 719 | test() |