blob: a3c647538dec748084c331975fb5e304f02d677c [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""An FTP client class and some helper functions.
2
Barry Warsaw100d81e2000-09-01 06:09:23 +00003Based on RFC 959: File Transfer Protocol (FTP), by J. Postel and J. Reynolds
Guido van Rossum1115ab21992-11-04 15:51:30 +00004
Guido van Rossumd2560b01996-05-28 23:41:25 +00005Example:
6
7>>> from ftplib import FTP
8>>> ftp = FTP('ftp.python.org') # connect to host, default port
Guido van Rossum24a64342001-12-28 20:54:55 +00009>>> ftp.login() # default, i.e.: user anonymous, passwd anonymous@
Guido van Rossum2f3941d1997-10-07 14:49:56 +000010'230 Guest login ok, access restrictions apply.'
Guido van Rossumd2560b01996-05-28 23:41:25 +000011>>> ftp.retrlines('LIST') # list directory contents
12total 9
13drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .
14drwxr-xr-x 8 root wheel 1024 Jan 3 1994 ..
15drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin
16drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc
17d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming
18drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib
19drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub
20drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr
21-rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg
Guido van Rossum2f3941d1997-10-07 14:49:56 +000022'226 Transfer complete.'
Guido van Rossumd2560b01996-05-28 23:41:25 +000023>>> ftp.quit()
Guido van Rossum2f3941d1997-10-07 14:49:56 +000024'221 Goodbye.'
Tim Peters88869f92001-01-14 23:36:06 +000025>>>
Guido van Rossumd2560b01996-05-28 23:41:25 +000026
27A nice test that reveals some of the network dialogue would be:
28python ftplib.py -d localhost -l -p -l
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000029"""
Guido van Rossumc567c601992-11-05 22:22:37 +000030
Tim Peters88869f92001-01-14 23:36:06 +000031#
Guido van Rossum98d9fd32000-02-28 15:12:25 +000032# Changes and improvements suggested by Steve Majewski.
33# Modified by Jack to work on the mac.
34# Modified by Siebren to support docstrings and PASV.
Gregory P. Smithc64386b2008-01-22 00:19:41 +000035# Modified by Phil Schwartz to add storbinary and storlines callbacks.
Guido van Rossum98d9fd32000-02-28 15:12:25 +000036#
Guido van Rossumc567c601992-11-05 22:22:37 +000037
Guido van Rossum1115ab21992-11-04 15:51:30 +000038import os
39import sys
Guido van Rossum1115ab21992-11-04 15:51:30 +000040
Guido van Rossumb6775db1994-08-01 11:34:53 +000041# Import SOCKS module if it exists, else standard socket module socket
42try:
Tim Peters88869f92001-01-14 23:36:06 +000043 import SOCKS; socket = SOCKS; del SOCKS # import SOCKS as socket
44 from socket import getfqdn; socket.getfqdn = getfqdn; del getfqdn
Guido van Rossumb6775db1994-08-01 11:34:53 +000045except ImportError:
Tim Peters88869f92001-01-14 23:36:06 +000046 import socket
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000047from socket import _GLOBAL_DEFAULT_TIMEOUT
Guido van Rossumb6775db1994-08-01 11:34:53 +000048
Skip Montanaroeccd02a2001-01-20 23:34:12 +000049__all__ = ["FTP","Netrc"]
Guido van Rossum1115ab21992-11-04 15:51:30 +000050
Guido van Rossumd3166071993-05-24 14:16:22 +000051# Magic number from <socket.h>
Tim Peters88869f92001-01-14 23:36:06 +000052MSG_OOB = 0x1 # Process data out of band
Guido van Rossumd3166071993-05-24 14:16:22 +000053
54
Guido van Rossumc567c601992-11-05 22:22:37 +000055# The standard FTP server control port
Guido van Rossum1115ab21992-11-04 15:51:30 +000056FTP_PORT = 21
Barry Warsawd6fddf32013-09-25 09:36:58 -040057# The sizehint parameter passed to readline() calls
58MAXLINE = 8192
Guido van Rossum1115ab21992-11-04 15:51:30 +000059
60
Guido van Rossum21974791992-11-06 13:34:17 +000061# Exception raised when an error or invalid response is received
Fred Drake227b1202000-08-17 05:06:49 +000062class Error(Exception): pass
Tim Peters88869f92001-01-14 23:36:06 +000063class error_reply(Error): pass # unexpected [123]xx reply
64class error_temp(Error): pass # 4xx errors
65class error_perm(Error): pass # 5xx errors
66class error_proto(Error): pass # response does not begin with [1-5]
Guido van Rossum1115ab21992-11-04 15:51:30 +000067
68
Guido van Rossum21974791992-11-06 13:34:17 +000069# All exceptions (hopefully) that may be raised here and that aren't
70# (always) programming errors on our side
Gregory P. Smithe6c03032008-04-12 22:24:04 +000071all_errors = (Error, IOError, EOFError)
Guido van Rossum21974791992-11-06 13:34:17 +000072
73
Guido van Rossum1115ab21992-11-04 15:51:30 +000074# Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)
75CRLF = '\r\n'
76
Guido van Rossum1115ab21992-11-04 15:51:30 +000077# The class itself
78class FTP:
79
Tim Peters88869f92001-01-14 23:36:06 +000080 '''An FTP client class.
Guido van Rossumd2560b01996-05-28 23:41:25 +000081
Facundo Batista3f100992007-03-26 20:56:09 +000082 To create a connection, call the class using these arguments:
83 host, user, passwd, acct, timeout
84
85 The first four arguments are all strings, and have default value ''.
86 timeout must be numeric and defaults to None if not passed,
87 meaning that no timeout will be set on any ftp socket(s)
88 If a timeout is passed, then this is now the default timeout for all ftp
89 socket operations for this instance.
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000090
Tim Peters88869f92001-01-14 23:36:06 +000091 Then use self.connect() with optional host and port argument.
Guido van Rossumd2560b01996-05-28 23:41:25 +000092
Tim Peters88869f92001-01-14 23:36:06 +000093 To download a file, use ftp.retrlines('RETR ' + filename),
94 or ftp.retrbinary() with slightly different arguments.
95 To upload a file, use ftp.storlines() or ftp.storbinary(),
96 which have an open file as argument (see their definitions
97 below for details).
98 The download/upload functions first issue appropriate TYPE
99 and PORT or PASV commands.
Guido van Rossumd2560b01996-05-28 23:41:25 +0000100'''
101
Fred Drake9c98a422001-02-28 21:46:37 +0000102 debugging = 0
103 host = ''
104 port = FTP_PORT
Barry Warsawd6fddf32013-09-25 09:36:58 -0400105 maxline = MAXLINE
Fred Drake9c98a422001-02-28 21:46:37 +0000106 sock = None
107 file = None
108 welcome = None
109 passiveserver = 1
110
Tim Peters88869f92001-01-14 23:36:06 +0000111 # Initialization method (called by class instantiation).
112 # Initialize host to localhost, port to standard ftp port
113 # Optional arguments are host (for connect()),
114 # and user, passwd, acct (for login())
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000115 def __init__(self, host='', user='', passwd='', acct='',
116 timeout=_GLOBAL_DEFAULT_TIMEOUT):
Facundo Batista3f100992007-03-26 20:56:09 +0000117 self.timeout = timeout
Tim Peters88869f92001-01-14 23:36:06 +0000118 if host:
Fred Drake9c98a422001-02-28 21:46:37 +0000119 self.connect(host)
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000120 if user:
Facundo Batista3f100992007-03-26 20:56:09 +0000121 self.login(user, passwd, acct)
Guido van Rossum52fc1f61993-06-17 12:38:10 +0000122
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000123 def connect(self, host='', port=0, timeout=-999):
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000124 '''Connect to host. Arguments are:
Facundo Batista3f100992007-03-26 20:56:09 +0000125 - host: hostname to connect to (string, default previous host)
126 - port: port to connect to (integer, default previous port)
127 '''
128 if host != '':
129 self.host = host
130 if port > 0:
131 self.port = port
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000132 if timeout != -999:
Facundo Batista93c33682007-03-30 13:00:35 +0000133 self.timeout = timeout
Facundo Batista3f100992007-03-26 20:56:09 +0000134 self.sock = socket.create_connection((self.host, self.port), self.timeout)
135 self.af = self.sock.family
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000136 self.file = self.sock.makefile('rb')
137 self.welcome = self.getresp()
138 return self.welcome
Guido van Rossum1115ab21992-11-04 15:51:30 +0000139
Tim Peters88869f92001-01-14 23:36:06 +0000140 def getwelcome(self):
141 '''Get the welcome message from the server.
142 (this is read and squirreled away by connect())'''
143 if self.debugging:
144 print '*welcome*', self.sanitize(self.welcome)
145 return self.welcome
Guido van Rossum1115ab21992-11-04 15:51:30 +0000146
Tim Peters88869f92001-01-14 23:36:06 +0000147 def set_debuglevel(self, level):
148 '''Set the debugging level.
149 The required argument level means:
150 0: no debugging output (default)
151 1: print commands and responses but not body text etc.
152 2: also print raw lines read and sent before stripping CR/LF'''
153 self.debugging = level
154 debug = set_debuglevel
Guido van Rossum1115ab21992-11-04 15:51:30 +0000155
Tim Peters88869f92001-01-14 23:36:06 +0000156 def set_pasv(self, val):
157 '''Use passive or active mode for data transfers.
158 With a false argument, use the normal PORT mode,
159 With a true argument, use the PASV command.'''
160 self.passiveserver = val
Guido van Rossumd2560b01996-05-28 23:41:25 +0000161
Tim Peters88869f92001-01-14 23:36:06 +0000162 # Internal: "sanitize" a string for printing
163 def sanitize(self, s):
164 if s[:5] == 'pass ' or s[:5] == 'PASS ':
165 i = len(s)
166 while i > 5 and s[i-1] in '\r\n':
167 i = i-1
168 s = s[:5] + '*'*(i-5) + s[i:]
Walter Dörwald70a6b492004-02-12 17:35:32 +0000169 return repr(s)
Guido van Rossumebaf1041995-05-05 15:54:14 +0000170
Tim Peters88869f92001-01-14 23:36:06 +0000171 # Internal: send one line to the server, appending CRLF
172 def putline(self, line):
173 line = line + CRLF
174 if self.debugging > 1: print '*put*', self.sanitize(line)
Martin v. Löwise12454f2002-02-16 23:06:19 +0000175 self.sock.sendall(line)
Guido van Rossum1115ab21992-11-04 15:51:30 +0000176
Tim Peters88869f92001-01-14 23:36:06 +0000177 # Internal: send one command to the server (through putline())
178 def putcmd(self, line):
179 if self.debugging: print '*cmd*', self.sanitize(line)
180 self.putline(line)
Guido van Rossum1115ab21992-11-04 15:51:30 +0000181
Tim Peters88869f92001-01-14 23:36:06 +0000182 # Internal: return one line from the server, stripping CRLF.
183 # Raise EOFError if the connection is closed
184 def getline(self):
Barry Warsawd6fddf32013-09-25 09:36:58 -0400185 line = self.file.readline(self.maxline + 1)
186 if len(line) > self.maxline:
187 raise Error("got more than %d bytes" % self.maxline)
Tim Peters88869f92001-01-14 23:36:06 +0000188 if self.debugging > 1:
189 print '*get*', self.sanitize(line)
190 if not line: raise EOFError
191 if line[-2:] == CRLF: line = line[:-2]
192 elif line[-1:] in CRLF: line = line[:-1]
193 return line
Guido van Rossum1115ab21992-11-04 15:51:30 +0000194
Tim Peters88869f92001-01-14 23:36:06 +0000195 # Internal: get a response from the server, which may possibly
196 # consist of multiple lines. Return a single string with no
197 # trailing CRLF. If the response consists of multiple lines,
198 # these are separated by '\n' characters in the string
199 def getmultiline(self):
200 line = self.getline()
201 if line[3:4] == '-':
202 code = line[:3]
203 while 1:
204 nextline = self.getline()
205 line = line + ('\n' + nextline)
206 if nextline[:3] == code and \
207 nextline[3:4] != '-':
208 break
209 return line
Guido van Rossum1115ab21992-11-04 15:51:30 +0000210
Tim Peters88869f92001-01-14 23:36:06 +0000211 # Internal: get a response from the server.
212 # Raise various errors if the response indicates an error
213 def getresp(self):
214 resp = self.getmultiline()
215 if self.debugging: print '*resp*', self.sanitize(resp)
216 self.lastresp = resp[:3]
217 c = resp[:1]
Raymond Hettingerc88a6c72005-04-05 04:31:09 +0000218 if c in ('1', '2', '3'):
219 return resp
Tim Peters88869f92001-01-14 23:36:06 +0000220 if c == '4':
221 raise error_temp, resp
222 if c == '5':
223 raise error_perm, resp
Raymond Hettingerc88a6c72005-04-05 04:31:09 +0000224 raise error_proto, resp
Guido van Rossum1115ab21992-11-04 15:51:30 +0000225
Tim Peters88869f92001-01-14 23:36:06 +0000226 def voidresp(self):
227 """Expect a response beginning with '2'."""
228 resp = self.getresp()
Georg Brandlf7a1efc2009-04-05 10:51:10 +0000229 if resp[:1] != '2':
Tim Peters88869f92001-01-14 23:36:06 +0000230 raise error_reply, resp
231 return resp
Guido van Rossumc567c601992-11-05 22:22:37 +0000232
Tim Peters88869f92001-01-14 23:36:06 +0000233 def abort(self):
234 '''Abort a file transfer. Uses out-of-band data.
235 This does not follow the procedure from the RFC to send Telnet
236 IP and Synch; that doesn't seem to work with the servers I've
237 tried. Instead, just send the ABOR command as OOB data.'''
238 line = 'ABOR' + CRLF
239 if self.debugging > 1: print '*put urgent*', self.sanitize(line)
Martin v. Löwise12454f2002-02-16 23:06:19 +0000240 self.sock.sendall(line, MSG_OOB)
Tim Peters88869f92001-01-14 23:36:06 +0000241 resp = self.getmultiline()
Giampaolo Rodolàfa1520a2010-04-18 13:13:54 +0000242 if resp[:3] not in ('426', '225', '226'):
Tim Peters88869f92001-01-14 23:36:06 +0000243 raise error_proto, resp
Guido van Rossumd3166071993-05-24 14:16:22 +0000244
Tim Peters88869f92001-01-14 23:36:06 +0000245 def sendcmd(self, cmd):
246 '''Send a command and return the response.'''
247 self.putcmd(cmd)
248 return self.getresp()
Guido van Rossum1115ab21992-11-04 15:51:30 +0000249
Tim Peters88869f92001-01-14 23:36:06 +0000250 def voidcmd(self, cmd):
251 """Send a command and expect a response beginning with '2'."""
252 self.putcmd(cmd)
253 return self.voidresp()
Guido van Rossumc567c601992-11-05 22:22:37 +0000254
Tim Peters88869f92001-01-14 23:36:06 +0000255 def sendport(self, host, port):
256 '''Send a PORT command with the current host and the given
257 port number.
258 '''
Eric S. Raymondc95bf692001-02-09 10:06:47 +0000259 hbytes = host.split('.')
Benjamin Petersondee0b172008-09-27 22:08:12 +0000260 pbytes = [repr(port//256), repr(port%256)]
Tim Peters88869f92001-01-14 23:36:06 +0000261 bytes = hbytes + pbytes
Eric S. Raymondc95bf692001-02-09 10:06:47 +0000262 cmd = 'PORT ' + ','.join(bytes)
Tim Peters88869f92001-01-14 23:36:06 +0000263 return self.voidcmd(cmd)
Guido van Rossum1115ab21992-11-04 15:51:30 +0000264
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000265 def sendeprt(self, host, port):
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000266 '''Send a EPRT command with the current host and the given port number.'''
267 af = 0
268 if self.af == socket.AF_INET:
269 af = 1
270 if self.af == socket.AF_INET6:
271 af = 2
272 if af == 0:
273 raise error_proto, 'unsupported address family'
Walter Dörwald70a6b492004-02-12 17:35:32 +0000274 fields = ['', repr(af), host, repr(port), '']
Neal Norwitz7ce734c2002-05-31 14:13:04 +0000275 cmd = 'EPRT ' + '|'.join(fields)
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000276 return self.voidcmd(cmd)
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000277
Tim Peters88869f92001-01-14 23:36:06 +0000278 def makeport(self):
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000279 '''Create a new socket and send a PORT command for it.'''
Martin v. Löwis2ad25692001-07-31 08:40:21 +0000280 msg = "getaddrinfo returns an empty list"
Martin v. Löwis322c0d12001-10-07 08:53:32 +0000281 sock = None
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000282 for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
283 af, socktype, proto, canonname, sa = res
284 try:
285 sock = socket.socket(af, socktype, proto)
286 sock.bind(sa)
287 except socket.error, msg:
Martin v. Löwis322c0d12001-10-07 08:53:32 +0000288 if sock:
289 sock.close()
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000290 sock = None
291 continue
292 break
293 if not sock:
294 raise socket.error, msg
295 sock.listen(1)
296 port = sock.getsockname()[1] # Get proper port
297 host = self.sock.getsockname()[0] # Get proper host
298 if self.af == socket.AF_INET:
299 resp = self.sendport(host, port)
300 else:
301 resp = self.sendeprt(host, port)
Giampaolo Rodolàa2eb7f62010-04-19 21:56:45 +0000302 if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
303 sock.settimeout(self.timeout)
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000304 return sock
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000305
306 def makepasv(self):
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000307 if self.af == socket.AF_INET:
308 host, port = parse227(self.sendcmd('PASV'))
309 else:
310 host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
311 return host, port
Guido van Rossum1115ab21992-11-04 15:51:30 +0000312
Tim Peters88869f92001-01-14 23:36:06 +0000313 def ntransfercmd(self, cmd, rest=None):
314 """Initiate a transfer over the data connection.
Barry Warsaw100d81e2000-09-01 06:09:23 +0000315
Tim Peters88869f92001-01-14 23:36:06 +0000316 If the transfer is active, send a port command and the
317 transfer command, and accept the connection. If the server is
318 passive, send a pasv command, connect to it, and start the
319 transfer command. Either way, return the socket for the
320 connection and the expected size of the transfer. The
321 expected size may be None if it could not be determined.
Barry Warsaw100d81e2000-09-01 06:09:23 +0000322
Tim Peters88869f92001-01-14 23:36:06 +0000323 Optional `rest' argument can be a string that is sent as the
Gregory P. Smith2230bcf2008-01-22 23:15:34 +0000324 argument to a REST command. This is essentially a server
Tim Peters88869f92001-01-14 23:36:06 +0000325 marker used to tell the server to skip over any data up to the
326 given marker.
327 """
328 size = None
329 if self.passiveserver:
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000330 host, port = self.makepasv()
Facundo Batista92493122007-06-06 15:13:37 +0000331 conn = socket.create_connection((host, port), self.timeout)
Tim Peters88869f92001-01-14 23:36:06 +0000332 if rest is not None:
333 self.sendcmd("REST %s" % rest)
334 resp = self.sendcmd(cmd)
Martin v. Löwis36cbc082006-11-12 18:48:13 +0000335 # Some servers apparently send a 200 reply to
336 # a LIST or STOR command, before the 150 reply
337 # (and way before the 226 reply). This seems to
338 # be in violation of the protocol (which only allows
339 # 1xx or error messages for LIST), so we just discard
340 # this response.
341 if resp[0] == '2':
Tim Petersf733abb2007-01-30 03:03:46 +0000342 resp = self.getresp()
Tim Peters88869f92001-01-14 23:36:06 +0000343 if resp[0] != '1':
344 raise error_reply, resp
345 else:
346 sock = self.makeport()
347 if rest is not None:
348 self.sendcmd("REST %s" % rest)
349 resp = self.sendcmd(cmd)
Martin v. Löwis36cbc082006-11-12 18:48:13 +0000350 # See above.
351 if resp[0] == '2':
Tim Petersf733abb2007-01-30 03:03:46 +0000352 resp = self.getresp()
Tim Peters88869f92001-01-14 23:36:06 +0000353 if resp[0] != '1':
354 raise error_reply, resp
355 conn, sockaddr = sock.accept()
Giampaolo Rodolàa2eb7f62010-04-19 21:56:45 +0000356 if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
357 conn.settimeout(self.timeout)
Tim Peters88869f92001-01-14 23:36:06 +0000358 if resp[:3] == '150':
359 # this is conditional in case we received a 125
360 size = parse150(resp)
361 return conn, size
Fred Drake4de02d91997-01-10 18:26:09 +0000362
Tim Peters88869f92001-01-14 23:36:06 +0000363 def transfercmd(self, cmd, rest=None):
Guido van Rossumb6aca6a2001-10-16 19:45:52 +0000364 """Like ntransfercmd() but returns only the socket."""
Tim Peters88869f92001-01-14 23:36:06 +0000365 return self.ntransfercmd(cmd, rest)[0]
Guido van Rossumc567c601992-11-05 22:22:37 +0000366
Tim Peters88869f92001-01-14 23:36:06 +0000367 def login(self, user = '', passwd = '', acct = ''):
368 '''Login, default anonymous.'''
369 if not user: user = 'anonymous'
370 if not passwd: passwd = ''
371 if not acct: acct = ''
372 if user == 'anonymous' and passwd in ('', '-'):
Tim Peterse4418602002-02-16 07:34:19 +0000373 # If there is no anonymous ftp password specified
374 # then we'll just use anonymous@
375 # We don't send any other thing because:
376 # - We want to remain anonymous
377 # - We want to stop SPAM
378 # - We don't want to let ftp sites to discriminate by the user,
379 # host or country.
Guido van Rossumc33e0772001-12-28 20:54:28 +0000380 passwd = passwd + 'anonymous@'
Tim Peters88869f92001-01-14 23:36:06 +0000381 resp = self.sendcmd('USER ' + user)
382 if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd)
383 if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct)
384 if resp[0] != '2':
385 raise error_reply, resp
386 return resp
Guido van Rossumc567c601992-11-05 22:22:37 +0000387
Tim Peters88869f92001-01-14 23:36:06 +0000388 def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000389 """Retrieve data in binary mode. A new port is created for you.
Barry Warsaw100d81e2000-09-01 06:09:23 +0000390
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000391 Args:
392 cmd: A RETR command.
393 callback: A single parameter callable to be called on each
394 block of data read.
395 blocksize: The maximum number of bytes to read from the
396 socket at one time. [default: 8192]
397 rest: Passed to transfercmd(). [default: None]
Guido van Rossum1115ab21992-11-04 15:51:30 +0000398
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000399 Returns:
400 The response code.
Tim Peters88869f92001-01-14 23:36:06 +0000401 """
402 self.voidcmd('TYPE I')
403 conn = self.transfercmd(cmd, rest)
404 while 1:
405 data = conn.recv(blocksize)
406 if not data:
407 break
408 callback(data)
409 conn.close()
410 return self.voidresp()
Guido van Rossum1115ab21992-11-04 15:51:30 +0000411
Tim Peters88869f92001-01-14 23:36:06 +0000412 def retrlines(self, cmd, callback = None):
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000413 """Retrieve data in line mode. A new port is created for you.
414
415 Args:
Gregory P. Smith2230bcf2008-01-22 23:15:34 +0000416 cmd: A RETR, LIST, NLST, or MLSD command.
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000417 callback: An optional single parameter callable that is called
418 for each line with the trailing CRLF stripped.
419 [default: print_line()]
420
421 Returns:
422 The response code.
423 """
Raymond Hettingere874fc32002-05-12 05:53:51 +0000424 if callback is None: callback = print_line
Tim Peters88869f92001-01-14 23:36:06 +0000425 resp = self.sendcmd('TYPE A')
426 conn = self.transfercmd(cmd)
427 fp = conn.makefile('rb')
428 while 1:
Barry Warsawd6fddf32013-09-25 09:36:58 -0400429 line = fp.readline(self.maxline + 1)
430 if len(line) > self.maxline:
431 raise Error("got more than %d bytes" % self.maxline)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000432 if self.debugging > 2: print '*retr*', repr(line)
Tim Peters88869f92001-01-14 23:36:06 +0000433 if not line:
434 break
435 if line[-2:] == CRLF:
436 line = line[:-2]
437 elif line[-1:] == '\n':
438 line = line[:-1]
439 callback(line)
440 fp.close()
441 conn.close()
442 return self.voidresp()
Guido van Rossumc567c601992-11-05 22:22:37 +0000443
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000444 def storbinary(self, cmd, fp, blocksize=8192, callback=None):
445 """Store a file in binary mode. A new port is created for you.
446
447 Args:
448 cmd: A STOR command.
449 fp: A file-like object with a read(num_bytes) method.
450 blocksize: The maximum data size to read from fp and send over
451 the connection at once. [default: 8192]
452 callback: An optional single parameter callable that is called on
453 on each block of data after it is sent. [default: None]
454
455 Returns:
456 The response code.
457 """
Tim Peters88869f92001-01-14 23:36:06 +0000458 self.voidcmd('TYPE I')
459 conn = self.transfercmd(cmd)
460 while 1:
461 buf = fp.read(blocksize)
462 if not buf: break
Martin v. Löwise12454f2002-02-16 23:06:19 +0000463 conn.sendall(buf)
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000464 if callback: callback(buf)
Tim Peters88869f92001-01-14 23:36:06 +0000465 conn.close()
466 return self.voidresp()
Guido van Rossumc567c601992-11-05 22:22:37 +0000467
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000468 def storlines(self, cmd, fp, callback=None):
469 """Store a file in line mode. A new port is created for you.
470
471 Args:
472 cmd: A STOR command.
473 fp: A file-like object with a readline() method.
474 callback: An optional single parameter callable that is called on
475 on each line after it is sent. [default: None]
476
477 Returns:
478 The response code.
479 """
Tim Peters88869f92001-01-14 23:36:06 +0000480 self.voidcmd('TYPE A')
481 conn = self.transfercmd(cmd)
482 while 1:
Barry Warsawd6fddf32013-09-25 09:36:58 -0400483 buf = fp.readline(self.maxline + 1)
484 if len(buf) > self.maxline:
485 raise Error("got more than %d bytes" % self.maxline)
Tim Peters88869f92001-01-14 23:36:06 +0000486 if not buf: break
487 if buf[-2:] != CRLF:
488 if buf[-1] in CRLF: buf = buf[:-1]
489 buf = buf + CRLF
Martin v. Löwise12454f2002-02-16 23:06:19 +0000490 conn.sendall(buf)
Gregory P. Smithc64386b2008-01-22 00:19:41 +0000491 if callback: callback(buf)
Tim Peters88869f92001-01-14 23:36:06 +0000492 conn.close()
493 return self.voidresp()
Guido van Rossum0eaa74b1996-01-25 18:37:21 +0000494
Tim Peters88869f92001-01-14 23:36:06 +0000495 def acct(self, password):
496 '''Send new account name.'''
497 cmd = 'ACCT ' + password
498 return self.voidcmd(cmd)
Guido van Rossumc567c601992-11-05 22:22:37 +0000499
Tim Peters88869f92001-01-14 23:36:06 +0000500 def nlst(self, *args):
501 '''Return a list of files in a given directory (default the current).'''
502 cmd = 'NLST'
503 for arg in args:
504 cmd = cmd + (' ' + arg)
505 files = []
506 self.retrlines(cmd, files.append)
507 return files
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000508
Tim Peters88869f92001-01-14 23:36:06 +0000509 def dir(self, *args):
510 '''List a directory in long form.
511 By default list current directory to stdout.
512 Optional last argument is callback function; all
513 non-empty arguments before it are concatenated to the
514 LIST command. (This *should* only be used for a pathname.)'''
515 cmd = 'LIST'
516 func = None
517 if args[-1:] and type(args[-1]) != type(''):
518 args, func = args[:-1], args[-1]
519 for arg in args:
520 if arg:
521 cmd = cmd + (' ' + arg)
522 self.retrlines(cmd, func)
Guido van Rossumc567c601992-11-05 22:22:37 +0000523
Tim Peters88869f92001-01-14 23:36:06 +0000524 def rename(self, fromname, toname):
525 '''Rename a file.'''
526 resp = self.sendcmd('RNFR ' + fromname)
527 if resp[0] != '3':
528 raise error_reply, resp
529 return self.voidcmd('RNTO ' + toname)
Guido van Rossuma61bdeb1995-10-11 17:36:31 +0000530
Tim Peters88869f92001-01-14 23:36:06 +0000531 def delete(self, filename):
532 '''Delete a file.'''
533 resp = self.sendcmd('DELE ' + filename)
534 if resp[:3] in ('250', '200'):
535 return resp
Tim Peters88869f92001-01-14 23:36:06 +0000536 else:
537 raise error_reply, resp
Guido van Rossum02cf5821993-05-17 08:00:02 +0000538
Tim Peters88869f92001-01-14 23:36:06 +0000539 def cwd(self, dirname):
540 '''Change to a directory.'''
541 if dirname == '..':
542 try:
543 return self.voidcmd('CDUP')
544 except error_perm, msg:
Martin v. Löwisb5255112002-03-10 15:59:58 +0000545 if msg.args[0][:3] != '500':
546 raise
Tim Peters88869f92001-01-14 23:36:06 +0000547 elif dirname == '':
548 dirname = '.' # does nothing, but could return error
549 cmd = 'CWD ' + dirname
550 return self.voidcmd(cmd)
Guido van Rossum17ed1ae1993-06-01 13:21:04 +0000551
Tim Peters88869f92001-01-14 23:36:06 +0000552 def size(self, filename):
553 '''Retrieve the size of a file.'''
Gregory P. Smith2230bcf2008-01-22 23:15:34 +0000554 # The SIZE command is defined in RFC-3659
Tim Peters88869f92001-01-14 23:36:06 +0000555 resp = self.sendcmd('SIZE ' + filename)
556 if resp[:3] == '213':
Guido van Rossumb6aca6a2001-10-16 19:45:52 +0000557 s = resp[3:].strip()
558 try:
559 return int(s)
Guido van Rossum1f74cb32001-10-17 17:21:47 +0000560 except (OverflowError, ValueError):
Guido van Rossumb6aca6a2001-10-16 19:45:52 +0000561 return long(s)
Guido van Rossumc567c601992-11-05 22:22:37 +0000562
Tim Peters88869f92001-01-14 23:36:06 +0000563 def mkd(self, dirname):
564 '''Make a directory, return its full pathname.'''
565 resp = self.sendcmd('MKD ' + dirname)
566 return parse257(resp)
Guido van Rossum98245091998-02-19 21:15:44 +0000567
Tim Peters88869f92001-01-14 23:36:06 +0000568 def rmd(self, dirname):
569 '''Remove a directory.'''
570 return self.voidcmd('RMD ' + dirname)
Guido van Rossum1115ab21992-11-04 15:51:30 +0000571
Tim Peters88869f92001-01-14 23:36:06 +0000572 def pwd(self):
573 '''Return current working directory.'''
574 resp = self.sendcmd('PWD')
575 return parse257(resp)
Guido van Rossum17ed1ae1993-06-01 13:21:04 +0000576
Tim Peters88869f92001-01-14 23:36:06 +0000577 def quit(self):
578 '''Quit, and close the connection.'''
579 resp = self.voidcmd('QUIT')
580 self.close()
581 return resp
582
583 def close(self):
584 '''Close the connection without assuming anything about it.'''
Fred Drake9c98a422001-02-28 21:46:37 +0000585 if self.file:
586 self.file.close()
587 self.sock.close()
588 self.file = self.sock = None
Guido van Rossumc567c601992-11-05 22:22:37 +0000589
590
Guido van Rossumacfb82a1997-10-22 20:49:52 +0000591_150_re = None
Fred Drake4de02d91997-01-10 18:26:09 +0000592
593def parse150(resp):
Tim Peters88869f92001-01-14 23:36:06 +0000594 '''Parse the '150' response for a RETR request.
595 Returns the expected transfer size or None; size is not guaranteed to
596 be present in the 150 message.
597 '''
598 if resp[:3] != '150':
599 raise error_reply, resp
600 global _150_re
601 if _150_re is None:
602 import re
603 _150_re = re.compile("150 .* \((\d+) bytes\)", re.IGNORECASE)
604 m = _150_re.match(resp)
Guido van Rossumb6aca6a2001-10-16 19:45:52 +0000605 if not m:
606 return None
607 s = m.group(1)
608 try:
609 return int(s)
Guido van Rossum1f74cb32001-10-17 17:21:47 +0000610 except (OverflowError, ValueError):
Guido van Rossumb6aca6a2001-10-16 19:45:52 +0000611 return long(s)
Fred Drake4de02d91997-01-10 18:26:09 +0000612
613
Guido van Rossum70297d32001-08-17 17:24:29 +0000614_227_re = None
615
Guido van Rossumd2560b01996-05-28 23:41:25 +0000616def parse227(resp):
Tim Peters88869f92001-01-14 23:36:06 +0000617 '''Parse the '227' response for a PASV request.
618 Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)'
619 Return ('host.addr.as.numbers', port#) tuple.'''
Guido van Rossumd2560b01996-05-28 23:41:25 +0000620
Tim Peters88869f92001-01-14 23:36:06 +0000621 if resp[:3] != '227':
622 raise error_reply, resp
Guido van Rossum70297d32001-08-17 17:24:29 +0000623 global _227_re
624 if _227_re is None:
625 import re
626 _227_re = re.compile(r'(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)')
627 m = _227_re.search(resp)
628 if not m:
Tim Peters88869f92001-01-14 23:36:06 +0000629 raise error_proto, resp
Guido van Rossum70297d32001-08-17 17:24:29 +0000630 numbers = m.groups()
Eric S. Raymondc95bf692001-02-09 10:06:47 +0000631 host = '.'.join(numbers[:4])
632 port = (int(numbers[4]) << 8) + int(numbers[5])
Tim Peters88869f92001-01-14 23:36:06 +0000633 return host, port
Guido van Rossumd2560b01996-05-28 23:41:25 +0000634
635
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000636def parse229(resp, peer):
637 '''Parse the '229' response for a EPSV request.
638 Raises error_proto if it does not contain '(|||port|)'
639 Return ('host.addr.as.numbers', port#) tuple.'''
640
Raymond Hettingerc88a6c72005-04-05 04:31:09 +0000641 if resp[:3] != '229':
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000642 raise error_reply, resp
Neal Norwitz7ce734c2002-05-31 14:13:04 +0000643 left = resp.find('(')
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000644 if left < 0: raise error_proto, resp
Neal Norwitz7ce734c2002-05-31 14:13:04 +0000645 right = resp.find(')', left + 1)
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000646 if right < 0:
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000647 raise error_proto, resp # should contain '(|||port|)'
Raymond Hettingerc88a6c72005-04-05 04:31:09 +0000648 if resp[left + 1] != resp[right - 1]:
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000649 raise error_proto, resp
Walter Dörwalda401ae42002-06-03 10:41:45 +0000650 parts = resp[left + 1:right].split(resp[left+1])
Raymond Hettingerc88a6c72005-04-05 04:31:09 +0000651 if len(parts) != 5:
Martin v. Löwis4eb59402001-07-26 13:37:33 +0000652 raise error_proto, resp
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000653 host = peer[0]
Neal Norwitz7ce734c2002-05-31 14:13:04 +0000654 port = int(parts[3])
Martin v. Löwisa43c2f82001-07-24 20:34:08 +0000655 return host, port
656
657
Guido van Rossumc567c601992-11-05 22:22:37 +0000658def parse257(resp):
Tim Peters88869f92001-01-14 23:36:06 +0000659 '''Parse the '257' response for a MKD or PWD request.
660 This is a response to a MKD or PWD request: a directory name.
661 Returns the directoryname in the 257 reply.'''
Guido van Rossumd2560b01996-05-28 23:41:25 +0000662
Tim Peters88869f92001-01-14 23:36:06 +0000663 if resp[:3] != '257':
664 raise error_reply, resp
665 if resp[3:5] != ' "':
666 return '' # Not compliant to RFC 959, but UNIX ftpd does this
667 dirname = ''
668 i = 5
669 n = len(resp)
670 while i < n:
671 c = resp[i]
672 i = i+1
673 if c == '"':
674 if i >= n or resp[i] != '"':
675 break
676 i = i+1
677 dirname = dirname + c
678 return dirname
Guido van Rossum1115ab21992-11-04 15:51:30 +0000679
Guido van Rossum2f3941d1997-10-07 14:49:56 +0000680
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000681def print_line(line):
Tim Peters88869f92001-01-14 23:36:06 +0000682 '''Default retrlines callback to print a line.'''
683 print line
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000684
Guido van Rossum2f3941d1997-10-07 14:49:56 +0000685
Guido van Rossumd2560b01996-05-28 23:41:25 +0000686def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
Tim Peters88869f92001-01-14 23:36:06 +0000687 '''Copy file from one FTP-instance to another.'''
688 if not targetname: targetname = sourcename
689 type = 'TYPE ' + type
690 source.voidcmd(type)
691 target.voidcmd(type)
692 sourcehost, sourceport = parse227(source.sendcmd('PASV'))
693 target.sendport(sourcehost, sourceport)
694 # RFC 959: the user must "listen" [...] BEFORE sending the
695 # transfer request.
696 # So: STOR before RETR, because here the target is a "user".
697 treply = target.sendcmd('STOR ' + targetname)
698 if treply[:3] not in ('125', '150'): raise error_proto # RFC 959
699 sreply = source.sendcmd('RETR ' + sourcename)
700 if sreply[:3] not in ('125', '150'): raise error_proto # RFC 959
701 source.voidresp()
702 target.voidresp()
Guido van Rossum1115ab21992-11-04 15:51:30 +0000703
Tim Peters88869f92001-01-14 23:36:06 +0000704
Guido van Rossum56d1e3a1997-03-14 04:16:54 +0000705class Netrc:
Tim Peters88869f92001-01-14 23:36:06 +0000706 """Class to parse & provide access to 'netrc' format files.
Fred Drake475d51d1997-06-24 22:02:54 +0000707
Tim Peters88869f92001-01-14 23:36:06 +0000708 See the netrc(4) man page for information on the file format.
Guido van Rossum56d1e3a1997-03-14 04:16:54 +0000709
Tim Peters88869f92001-01-14 23:36:06 +0000710 WARNING: This class is obsolete -- use module netrc instead.
Guido van Rossumc822a451998-12-22 16:49:16 +0000711
Tim Peters88869f92001-01-14 23:36:06 +0000712 """
713 __defuser = None
714 __defpasswd = None
715 __defacct = None
Guido van Rossum56d1e3a1997-03-14 04:16:54 +0000716
Tim Peters88869f92001-01-14 23:36:06 +0000717 def __init__(self, filename=None):
Raymond Hettinger094662a2002-06-01 01:29:16 +0000718 if filename is None:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000719 if "HOME" in os.environ:
Tim Peters88869f92001-01-14 23:36:06 +0000720 filename = os.path.join(os.environ["HOME"],
721 ".netrc")
722 else:
723 raise IOError, \
724 "specify file to load or set $HOME"
725 self.__hosts = {}
726 self.__macros = {}
727 fp = open(filename, "r")
728 in_macro = 0
729 while 1:
730 line = fp.readline()
731 if not line: break
Eric S. Raymondc95bf692001-02-09 10:06:47 +0000732 if in_macro and line.strip():
Tim Peters88869f92001-01-14 23:36:06 +0000733 macro_lines.append(line)
734 continue
735 elif in_macro:
736 self.__macros[macro_name] = tuple(macro_lines)
737 in_macro = 0
Eric S. Raymondc95bf692001-02-09 10:06:47 +0000738 words = line.split()
Tim Peters88869f92001-01-14 23:36:06 +0000739 host = user = passwd = acct = None
740 default = 0
741 i = 0
742 while i < len(words):
743 w1 = words[i]
744 if i+1 < len(words):
745 w2 = words[i + 1]
746 else:
747 w2 = None
748 if w1 == 'default':
749 default = 1
750 elif w1 == 'machine' and w2:
Eric S. Raymondc95bf692001-02-09 10:06:47 +0000751 host = w2.lower()
Tim Peters88869f92001-01-14 23:36:06 +0000752 i = i + 1
753 elif w1 == 'login' and w2:
754 user = w2
755 i = i + 1
756 elif w1 == 'password' and w2:
757 passwd = w2
758 i = i + 1
759 elif w1 == 'account' and w2:
760 acct = w2
761 i = i + 1
762 elif w1 == 'macdef' and w2:
763 macro_name = w2
764 macro_lines = []
765 in_macro = 1
766 break
767 i = i + 1
768 if default:
769 self.__defuser = user or self.__defuser
770 self.__defpasswd = passwd or self.__defpasswd
771 self.__defacct = acct or self.__defacct
772 if host:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000773 if host in self.__hosts:
Tim Peters88869f92001-01-14 23:36:06 +0000774 ouser, opasswd, oacct = \
775 self.__hosts[host]
776 user = user or ouser
777 passwd = passwd or opasswd
778 acct = acct or oacct
779 self.__hosts[host] = user, passwd, acct
780 fp.close()
Guido van Rossum56d1e3a1997-03-14 04:16:54 +0000781
Tim Peters88869f92001-01-14 23:36:06 +0000782 def get_hosts(self):
783 """Return a list of hosts mentioned in the .netrc file."""
784 return self.__hosts.keys()
Guido van Rossum8ca84201998-03-26 20:56:10 +0000785
Tim Peters88869f92001-01-14 23:36:06 +0000786 def get_account(self, host):
787 """Returns login information for the named host.
Guido van Rossum8ca84201998-03-26 20:56:10 +0000788
Tim Peters88869f92001-01-14 23:36:06 +0000789 The return value is a triple containing userid,
790 password, and the accounting field.
Guido van Rossum8ca84201998-03-26 20:56:10 +0000791
Tim Peters88869f92001-01-14 23:36:06 +0000792 """
Eric S. Raymondc95bf692001-02-09 10:06:47 +0000793 host = host.lower()
Tim Peters88869f92001-01-14 23:36:06 +0000794 user = passwd = acct = None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000795 if host in self.__hosts:
Tim Peters88869f92001-01-14 23:36:06 +0000796 user, passwd, acct = self.__hosts[host]
797 user = user or self.__defuser
798 passwd = passwd or self.__defpasswd
799 acct = acct or self.__defacct
800 return user, passwd, acct
Guido van Rossum8ca84201998-03-26 20:56:10 +0000801
Tim Peters88869f92001-01-14 23:36:06 +0000802 def get_macros(self):
803 """Return a list of all defined macro names."""
804 return self.__macros.keys()
Guido van Rossum8ca84201998-03-26 20:56:10 +0000805
Tim Peters88869f92001-01-14 23:36:06 +0000806 def get_macro(self, macro):
807 """Return a sequence of lines which define a named macro."""
808 return self.__macros[macro]
Guido van Rossum56d1e3a1997-03-14 04:16:54 +0000809
Fred Drake475d51d1997-06-24 22:02:54 +0000810
Tim Peters88869f92001-01-14 23:36:06 +0000811
Guido van Rossum1115ab21992-11-04 15:51:30 +0000812def test():
Tim Peters88869f92001-01-14 23:36:06 +0000813 '''Test program.
Raymond Hettingerc88a6c72005-04-05 04:31:09 +0000814 Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
815
816 -d dir
817 -l list
818 -p password
819 '''
820
821 if len(sys.argv) < 2:
822 print test.__doc__
823 sys.exit(0)
Guido van Rossumd2560b01996-05-28 23:41:25 +0000824
Tim Peters88869f92001-01-14 23:36:06 +0000825 debugging = 0
826 rcfile = None
827 while sys.argv[1] == '-d':
828 debugging = debugging+1
829 del sys.argv[1]
830 if sys.argv[1][:2] == '-r':
831 # get name of alternate ~/.netrc file:
832 rcfile = sys.argv[1][2:]
833 del sys.argv[1]
834 host = sys.argv[1]
835 ftp = FTP(host)
836 ftp.set_debuglevel(debugging)
837 userid = passwd = acct = ''
838 try:
839 netrc = Netrc(rcfile)
840 except IOError:
841 if rcfile is not None:
842 sys.stderr.write("Could not open account file"
843 " -- using anonymous login.")
844 else:
845 try:
846 userid, passwd, acct = netrc.get_account(host)
847 except KeyError:
848 # no account for host
849 sys.stderr.write(
850 "No account -- using anonymous login.")
851 ftp.login(userid, passwd, acct)
852 for file in sys.argv[2:]:
853 if file[:2] == '-l':
854 ftp.dir(file[2:])
855 elif file[:2] == '-d':
856 cmd = 'CWD'
857 if file[2:]: cmd = cmd + ' ' + file[2:]
858 resp = ftp.sendcmd(cmd)
859 elif file == '-p':
860 ftp.set_pasv(not ftp.passiveserver)
861 else:
862 ftp.retrbinary('RETR ' + file, \
863 sys.stdout.write, 1024)
864 ftp.quit()
Guido van Rossum221ec0b1995-08-04 04:39:30 +0000865
866
867if __name__ == '__main__':
Tim Peters88869f92001-01-14 23:36:06 +0000868 test()