Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Lib / ftplib.py
blob63903d0879441b64205b49a349ffeac896290b39
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
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.
10 Example:
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
15 '230 Guest login ok, access restrictions apply.'
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
27 '226 Transfer complete.'
28 >>> ftp.quit()
29 '221 Goodbye.'
30 >>>
32 A nice test that reveals some of the network dialogue would be:
33 python ftplib.py -d localhost -l -p -l
34 '''
37 import os
38 import sys
39 import string
41 # Import SOCKS module if it exists, else standard socket module socket
42 try:
43 import SOCKS; socket = SOCKS
44 except ImportError:
45 import socket
48 # Magic number from <socket.h>
49 MSG_OOB = 0x1 # Process data out of band
52 # The standard FTP server control port
53 FTP_PORT = 21
56 # Exception raised when an error or invalid response is received
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]
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, \
66 socket.error, IOError, EOFError)
69 # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)
70 CRLF = '\r\n'
73 # The class itself
74 class FTP:
76 '''An FTP client class.
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.
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 '''
92 # Initialization method (called by class instantiation).
93 # Initialize host to localhost, port to standard ftp port
94 # Optional arguments are host (for connect()),
95 # and user, passwd, acct (for login())
96 def __init__(self, host = '', user = '', passwd = '', acct = ''):
97 # Initialize the instance to something mostly harmless
98 self.debugging = 0
99 self.host = ''
100 self.port = FTP_PORT
101 self.sock = None
102 self.file = None
103 self.welcome = None
104 resp = None
105 if host:
106 resp = self.connect(host)
107 if user: resp = self.login(user, passwd, acct)
109 def connect(self, host = '', port = 0):
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)'''
113 if host: self.host = host
114 if port: self.port = port
115 self.passiveserver = 0
116 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
117 self.sock.connect(self.host, self.port)
118 self.file = self.sock.makefile('rb')
119 self.welcome = self.getresp()
120 return self.welcome
122 def getwelcome(self):
123 '''Get the welcome message from the server.
124 (this is read and squirreled away by connect())'''
125 if self.debugging:
126 print '*welcome*', self.sanitize(self.welcome)
127 return self.welcome
129 def set_debuglevel(self, level):
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'''
135 self.debugging = level
136 debug = set_debuglevel
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
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`
153 # Internal: send one line to the server, appending CRLF
154 def putline(self, line):
155 line = line + CRLF
156 if self.debugging > 1: print '*put*', self.sanitize(line)
157 self.sock.send(line)
159 # Internal: send one command to the server (through putline())
160 def putcmd(self, line):
161 if self.debugging: print '*cmd*', self.sanitize(line)
162 self.putline(line)
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:
169 print '*get*', self.sanitize(line)
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
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
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()
195 if self.debugging: print '*resp*', self.sanitize(resp)
196 self.lastresp = resp[:3]
197 c = resp[:1]
198 if c == '4':
199 raise error_temp, resp
200 if c == '5':
201 raise error_perm, resp
202 if c not in '123':
203 raise error_proto, resp
204 return resp
206 def voidresp(self):
207 """Expect a response beginning with '2'."""
208 resp = self.getresp()
209 if resp[0] <> '2':
210 raise error_reply, resp
211 return resp
213 def abort(self):
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.'''
218 line = 'ABOR' + CRLF
219 if self.debugging > 1: print '*put urgent*', self.sanitize(line)
220 self.sock.send(line, MSG_OOB)
221 resp = self.getmultiline()
222 if resp[:3] not in ('426', '226'):
223 raise error_proto, resp
225 def sendcmd(self, cmd):
226 '''Send a command and return the response.'''
227 self.putcmd(cmd)
228 return self.getresp()
230 def voidcmd(self, cmd):
231 """Send a command and expect a response beginning with '2'."""
232 self.putcmd(cmd)
233 return self.voidresp()
235 def sendport(self, host, port):
236 '''Send a PORT command with the current host and the given port number.'''
237 hbytes = string.splitfields(host, '.')
238 pbytes = [`port/256`, `port%256`]
239 bytes = hbytes + pbytes
240 cmd = 'PORT ' + string.joinfields(bytes, ',')
241 return self.voidcmd(cmd)
243 def makeport(self):
244 '''Create a new socket and send a PORT command for it.'''
245 global nextport
246 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
247 sock.bind(('', 0))
248 sock.listen(1)
249 dummyhost, port = sock.getsockname() # Get proper port
250 host, dummyport = self.sock.getsockname() # Get proper host
251 resp = self.sendport(host, port)
252 return sock
254 def ntransfercmd(self, cmd):
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.
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
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()
277 if resp[:3] == '150':
278 # this is conditional in case we received a 125
279 size = parse150(resp)
280 return conn, size
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]
287 def login(self, user = '', passwd = '', acct = ''):
288 '''Login, default anonymous.'''
289 if not user: user = 'anonymous'
290 if not passwd: passwd = ''
291 if not acct: acct = ''
292 if user == 'anonymous' and passwd in ('', '-'):
293 thishost = socket.gethostname()
294 # Make sure it is fully qualified
295 if not '.' in thishost:
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
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....
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
320 return resp
322 def retrbinary(self, cmd, callback, blocksize=8192):
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'''
327 self.voidcmd('TYPE I')
328 conn = self.transfercmd(cmd)
329 while 1:
330 data = conn.recv(blocksize)
331 if not data:
332 break
333 callback(data)
334 conn.close()
335 return self.voidresp()
337 def retrlines(self, cmd, callback = None):
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.
342 print_lines is the default callback.'''
343 if not callback: callback = print_line
344 resp = self.sendcmd('TYPE A')
345 conn = self.transfercmd(cmd)
346 fp = conn.makefile('rb')
347 while 1:
348 line = fp.readline()
349 if self.debugging > 2: print '*retr*', `line`
350 if not line:
351 break
352 if line[-2:] == CRLF:
353 line = line[:-2]
354 elif line[:-1] == '\n':
355 line = line[:-1]
356 callback(line)
357 fp.close()
358 conn.close()
359 return self.voidresp()
361 def storbinary(self, cmd, fp, blocksize):
362 '''Store a file in binary mode.'''
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()
370 return self.voidresp()
372 def storlines(self, cmd, fp):
373 '''Store a file in line mode.'''
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()
384 return self.voidresp()
386 def acct(self, password):
387 '''Send new account name.'''
388 cmd = 'ACCT ' + password
389 return self.voidcmd(cmd)
391 def nlst(self, *args):
392 '''Return a list of files in a given directory (default the current).'''
393 cmd = 'NLST'
394 for arg in args:
395 cmd = cmd + (' ' + arg)
396 files = []
397 self.retrlines(cmd, files.append)
398 return files
400 def dir(self, *args):
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.)'''
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)
415 def rename(self, fromname, toname):
416 '''Rename a file.'''
417 resp = self.sendcmd('RNFR ' + fromname)
418 if resp[0] <> '3':
419 raise error_reply, resp
420 return self.voidcmd('RNTO ' + toname)
422 def delete(self, filename):
423 '''Delete a file.'''
424 resp = self.sendcmd('DELE ' + filename)
425 if resp[:3] == '250':
426 return resp
427 elif resp[:1] == '5':
428 raise error_perm, resp
429 else:
430 raise error_reply, resp
432 def cwd(self, dirname):
433 '''Change to a directory.'''
434 if dirname == '..':
435 try:
436 return self.voidcmd('CDUP')
437 except error_perm, msg:
438 if msg[:3] != '500':
439 raise error_perm, msg
440 cmd = 'CWD ' + dirname
441 return self.voidcmd(cmd)
443 def size(self, filename):
444 '''Retrieve the size of a file.'''
445 # Note that the RFC doesn't say anything about 'SIZE'
446 resp = self.sendcmd('SIZE ' + filename)
447 if resp[:3] == '213':
448 return string.atoi(string.strip(resp[3:]))
450 def mkd(self, dirname):
451 '''Make a directory, return its full pathname.'''
452 resp = self.sendcmd('MKD ' + dirname)
453 return parse257(resp)
455 def rmd(self, dirname):
456 '''Remove a directory.'''
457 return self.voidcmd('RMD ' + dirname)
459 def pwd(self):
460 '''Return current working directory.'''
461 resp = self.sendcmd('PWD')
462 return parse257(resp)
464 def quit(self):
465 '''Quit, and close the connection.'''
466 resp = self.voidcmd('QUIT')
467 self.close()
468 return resp
470 def close(self):
471 '''Close the connection without assuming anything about it.'''
472 self.file.close()
473 self.sock.close()
474 del self.file, self.sock
477 _150_re = None
479 def parse150(resp):
480 '''Parse the '150' response for a RETR request.
481 Returns the expected transfer size or None; size is not guaranteed to
482 be present in the 150 message.
484 if resp[:3] != '150':
485 raise error_reply, resp
486 global _150_re
487 if _150_re is None:
488 import re
489 _150_re = re.compile("150 .* \(([0-9][0-9]*) bytes\)",
490 re.IGNORECASE)
491 m = _150_re.match(resp)
492 if m:
493 return string.atoi(m.group(1))
494 return None
497 def parse227(resp):
498 '''Parse the '227' response for a PASV request.
499 Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)'
500 Return ('host.addr.as.numbers', port#) tuple.'''
502 if resp[:3] <> '227':
503 raise error_reply, resp
504 left = string.find(resp, '(')
505 if left < 0: raise error_proto, resp
506 right = string.find(resp, ')', left + 1)
507 if right < 0:
508 raise error_proto, resp # should contain '(h1,h2,h3,h4,p1,p2)'
509 numbers = string.split(resp[left+1:right], ',')
510 if len(numbers) <> 6:
511 raise error_proto, resp
512 host = string.join(numbers[:4], '.')
513 port = (string.atoi(numbers[4]) << 8) + string.atoi(numbers[5])
514 return host, port
517 def parse257(resp):
518 '''Parse the '257' response for a MKD or PWD request.
519 This is a response to a MKD or PWD request: a directory name.
520 Returns the directoryname in the 257 reply.'''
522 if resp[:3] <> '257':
523 raise error_reply, resp
524 if resp[3:5] <> ' "':
525 return '' # Not compliant to RFC 959, but UNIX ftpd does this
526 dirname = ''
527 i = 5
528 n = len(resp)
529 while i < n:
530 c = resp[i]
531 i = i+1
532 if c == '"':
533 if i >= n or resp[i] <> '"':
534 break
535 i = i+1
536 dirname = dirname + c
537 return dirname
540 def print_line(line):
541 '''Default retrlines callback to print a line.'''
542 print line
545 def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
546 '''Copy file from one FTP-instance to another.'''
547 if not targetname: targetname = sourcename
548 type = 'TYPE ' + type
549 source.voidcmd(type)
550 target.voidcmd(type)
551 sourcehost, sourceport = parse227(source.sendcmd('PASV'))
552 target.sendport(sourcehost, sourceport)
553 # RFC 959: the user must "listen" [...] BEFORE sending the
554 # transfer request.
555 # So: STOR before RETR, because here the target is a "user".
556 treply = target.sendcmd('STOR ' + targetname)
557 if treply[:3] not in ('125', '150'): raise error_proto # RFC 959
558 sreply = source.sendcmd('RETR ' + sourcename)
559 if sreply[:3] not in ('125', '150'): raise error_proto # RFC 959
560 source.voidresp()
561 target.voidresp()
564 class Netrc:
565 """Class to parse & provide access to 'netrc' format files.
567 See the netrc(4) man page for information on the file format.
570 __defuser = None
571 __defpasswd = None
572 __defacct = None
574 def __init__(self, filename=None):
575 if not filename:
576 if os.environ.has_key("HOME"):
577 filename = os.path.join(os.environ["HOME"],
578 ".netrc")
579 else:
580 raise IOError, \
581 "specify file to load or set $HOME"
582 self.__hosts = {}
583 self.__macros = {}
584 fp = open(filename, "r")
585 in_macro = 0
586 while 1:
587 line = fp.readline()
588 if not line: break
589 if in_macro and string.strip(line):
590 macro_lines.append(line)
591 continue
592 elif in_macro:
593 self.__macros[macro_name] = tuple(macro_lines)
594 in_macro = 0
595 words = string.split(line)
596 host = user = passwd = acct = None
597 default = 0
598 i = 0
599 while i < len(words):
600 w1 = words[i]
601 if i+1 < len(words):
602 w2 = words[i + 1]
603 else:
604 w2 = None
605 if w1 == 'default':
606 default = 1
607 elif w1 == 'machine' and w2:
608 host = string.lower(w2)
609 i = i + 1
610 elif w1 == 'login' and w2:
611 user = w2
612 i = i + 1
613 elif w1 == 'password' and w2:
614 passwd = w2
615 i = i + 1
616 elif w1 == 'account' and w2:
617 acct = w2
618 i = i + 1
619 elif w1 == 'macdef' and w2:
620 macro_name = w2
621 macro_lines = []
622 in_macro = 1
623 break
624 i = i + 1
625 if default:
626 self.__defuser = user or self.__defuser
627 self.__defpasswd = passwd or self.__defpasswd
628 self.__defacct = acct or self.__defacct
629 if host:
630 if self.__hosts.has_key(host):
631 ouser, opasswd, oacct = \
632 self.__hosts[host]
633 user = user or ouser
634 passwd = passwd or opasswd
635 acct = acct or oacct
636 self.__hosts[host] = user, passwd, acct
637 fp.close()
639 def get_hosts(self):
640 """Return a list of hosts mentioned in the .netrc file."""
641 return self.__hosts.keys()
643 def get_account(self, host):
644 """Returns login information for the named host.
646 The return value is a triple containing userid,
647 password, and the accounting field.
650 host = string.lower(host)
651 user = passwd = acct = None
652 if self.__hosts.has_key(host):
653 user, passwd, acct = self.__hosts[host]
654 user = user or self.__defuser
655 passwd = passwd or self.__defpasswd
656 acct = acct or self.__defacct
657 return user, passwd, acct
659 def get_macros(self):
660 """Return a list of all defined macro names."""
661 return self.__macros.keys()
663 def get_macro(self, macro):
664 """Return a sequence of lines which define a named macro."""
665 return self.__macros[macro]
669 def test():
670 '''Test program.
671 Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...'''
673 debugging = 0
674 rcfile = None
675 while sys.argv[1] == '-d':
676 debugging = debugging+1
677 del sys.argv[1]
678 if sys.argv[1][:2] == '-r':
679 # get name of alternate ~/.netrc file:
680 rcfile = sys.argv[1][2:]
681 del sys.argv[1]
682 host = sys.argv[1]
683 ftp = FTP(host)
684 ftp.set_debuglevel(debugging)
685 userid = passwd = acct = ''
686 try:
687 netrc = Netrc(rcfile)
688 except IOError:
689 if rcfile is not None:
690 sys.stderr.write("Could not open account file"
691 " -- using anonymous login.")
692 else:
693 try:
694 userid, passwd, acct = netrc.get_account(host)
695 except KeyError:
696 # no account for host
697 sys.stderr.write(
698 "No account -- using anonymous login.")
699 ftp.login(userid, passwd, acct)
700 for file in sys.argv[2:]:
701 if file[:2] == '-l':
702 ftp.dir(file[2:])
703 elif file[:2] == '-d':
704 cmd = 'CWD'
705 if file[2:]: cmd = cmd + ' ' + file[2:]
706 resp = ftp.sendcmd(cmd)
707 elif file == '-p':
708 ftp.set_pasv(not ftp.passiveserver)
709 else:
710 ftp.retrbinary('RETR ' + file, \
711 sys.stdout.write, 1024)
712 ftp.quit()
715 if __name__ == '__main__':
716 test()