1 | n/a | """An FTP client class and some helper functions. |
---|
2 | n/a | |
---|
3 | n/a | Based on RFC 959: File Transfer Protocol (FTP), by J. Postel and J. Reynolds |
---|
4 | n/a | |
---|
5 | n/a | Example: |
---|
6 | n/a | |
---|
7 | n/a | >>> from ftplib import FTP |
---|
8 | n/a | >>> ftp = FTP('ftp.python.org') # connect to host, default port |
---|
9 | n/a | >>> ftp.login() # default, i.e.: user anonymous, passwd anonymous@ |
---|
10 | n/a | '230 Guest login ok, access restrictions apply.' |
---|
11 | n/a | >>> ftp.retrlines('LIST') # list directory contents |
---|
12 | n/a | total 9 |
---|
13 | n/a | drwxr-xr-x 8 root wheel 1024 Jan 3 1994 . |
---|
14 | n/a | drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .. |
---|
15 | n/a | drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin |
---|
16 | n/a | drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc |
---|
17 | n/a | d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming |
---|
18 | n/a | drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib |
---|
19 | n/a | drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub |
---|
20 | n/a | drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr |
---|
21 | n/a | -rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg |
---|
22 | n/a | '226 Transfer complete.' |
---|
23 | n/a | >>> ftp.quit() |
---|
24 | n/a | '221 Goodbye.' |
---|
25 | n/a | >>> |
---|
26 | n/a | |
---|
27 | n/a | A nice test that reveals some of the network dialogue would be: |
---|
28 | n/a | python ftplib.py -d localhost -l -p -l |
---|
29 | n/a | """ |
---|
30 | n/a | |
---|
31 | n/a | # |
---|
32 | n/a | # Changes and improvements suggested by Steve Majewski. |
---|
33 | n/a | # Modified by Jack to work on the mac. |
---|
34 | n/a | # Modified by Siebren to support docstrings and PASV. |
---|
35 | n/a | # Modified by Phil Schwartz to add storbinary and storlines callbacks. |
---|
36 | n/a | # Modified by Giampaolo Rodola' to add TLS support. |
---|
37 | n/a | # |
---|
38 | n/a | |
---|
39 | n/a | import sys |
---|
40 | n/a | import socket |
---|
41 | n/a | from socket import _GLOBAL_DEFAULT_TIMEOUT |
---|
42 | n/a | |
---|
43 | n/a | __all__ = ["FTP", "error_reply", "error_temp", "error_perm", "error_proto", |
---|
44 | n/a | "all_errors"] |
---|
45 | n/a | |
---|
46 | n/a | # Magic number from <socket.h> |
---|
47 | n/a | MSG_OOB = 0x1 # Process data out of band |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | # The standard FTP server control port |
---|
51 | n/a | FTP_PORT = 21 |
---|
52 | n/a | # The sizehint parameter passed to readline() calls |
---|
53 | n/a | MAXLINE = 8192 |
---|
54 | n/a | |
---|
55 | n/a | |
---|
56 | n/a | # Exception raised when an error or invalid response is received |
---|
57 | n/a | class Error(Exception): pass |
---|
58 | n/a | class error_reply(Error): pass # unexpected [123]xx reply |
---|
59 | n/a | class error_temp(Error): pass # 4xx errors |
---|
60 | n/a | class error_perm(Error): pass # 5xx errors |
---|
61 | n/a | class error_proto(Error): pass # response does not begin with [1-5] |
---|
62 | n/a | |
---|
63 | n/a | |
---|
64 | n/a | # All exceptions (hopefully) that may be raised here and that aren't |
---|
65 | n/a | # (always) programming errors on our side |
---|
66 | n/a | all_errors = (Error, OSError, EOFError) |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) |
---|
70 | n/a | CRLF = '\r\n' |
---|
71 | n/a | B_CRLF = b'\r\n' |
---|
72 | n/a | |
---|
73 | n/a | # The class itself |
---|
74 | n/a | class FTP: |
---|
75 | n/a | |
---|
76 | n/a | '''An FTP client class. |
---|
77 | n/a | |
---|
78 | n/a | To create a connection, call the class using these arguments: |
---|
79 | n/a | host, user, passwd, acct, timeout |
---|
80 | n/a | |
---|
81 | n/a | The first four arguments are all strings, and have default value ''. |
---|
82 | n/a | timeout must be numeric and defaults to None if not passed, |
---|
83 | n/a | meaning that no timeout will be set on any ftp socket(s) |
---|
84 | n/a | If a timeout is passed, then this is now the default timeout for all ftp |
---|
85 | n/a | socket operations for this instance. |
---|
86 | n/a | |
---|
87 | n/a | Then use self.connect() with optional host and port argument. |
---|
88 | n/a | |
---|
89 | n/a | To download a file, use ftp.retrlines('RETR ' + filename), |
---|
90 | n/a | or ftp.retrbinary() with slightly different arguments. |
---|
91 | n/a | To upload a file, use ftp.storlines() or ftp.storbinary(), |
---|
92 | n/a | which have an open file as argument (see their definitions |
---|
93 | n/a | below for details). |
---|
94 | n/a | The download/upload functions first issue appropriate TYPE |
---|
95 | n/a | and PORT or PASV commands. |
---|
96 | n/a | ''' |
---|
97 | n/a | |
---|
98 | n/a | debugging = 0 |
---|
99 | n/a | host = '' |
---|
100 | n/a | port = FTP_PORT |
---|
101 | n/a | maxline = MAXLINE |
---|
102 | n/a | sock = None |
---|
103 | n/a | file = None |
---|
104 | n/a | welcome = None |
---|
105 | n/a | passiveserver = 1 |
---|
106 | n/a | encoding = "latin-1" |
---|
107 | n/a | |
---|
108 | n/a | # Initialization method (called by class instantiation). |
---|
109 | n/a | # Initialize host to localhost, port to standard ftp port |
---|
110 | n/a | # Optional arguments are host (for connect()), |
---|
111 | n/a | # and user, passwd, acct (for login()) |
---|
112 | n/a | def __init__(self, host='', user='', passwd='', acct='', |
---|
113 | n/a | timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None): |
---|
114 | n/a | self.source_address = source_address |
---|
115 | n/a | self.timeout = timeout |
---|
116 | n/a | if host: |
---|
117 | n/a | self.connect(host) |
---|
118 | n/a | if user: |
---|
119 | n/a | self.login(user, passwd, acct) |
---|
120 | n/a | |
---|
121 | n/a | def __enter__(self): |
---|
122 | n/a | return self |
---|
123 | n/a | |
---|
124 | n/a | # Context management protocol: try to quit() if active |
---|
125 | n/a | def __exit__(self, *args): |
---|
126 | n/a | if self.sock is not None: |
---|
127 | n/a | try: |
---|
128 | n/a | self.quit() |
---|
129 | n/a | except (OSError, EOFError): |
---|
130 | n/a | pass |
---|
131 | n/a | finally: |
---|
132 | n/a | if self.sock is not None: |
---|
133 | n/a | self.close() |
---|
134 | n/a | |
---|
135 | n/a | def connect(self, host='', port=0, timeout=-999, source_address=None): |
---|
136 | n/a | '''Connect to host. Arguments are: |
---|
137 | n/a | - host: hostname to connect to (string, default previous host) |
---|
138 | n/a | - port: port to connect to (integer, default previous port) |
---|
139 | n/a | - timeout: the timeout to set against the ftp socket(s) |
---|
140 | n/a | - source_address: a 2-tuple (host, port) for the socket to bind |
---|
141 | n/a | to as its source address before connecting. |
---|
142 | n/a | ''' |
---|
143 | n/a | if host != '': |
---|
144 | n/a | self.host = host |
---|
145 | n/a | if port > 0: |
---|
146 | n/a | self.port = port |
---|
147 | n/a | if timeout != -999: |
---|
148 | n/a | self.timeout = timeout |
---|
149 | n/a | if source_address is not None: |
---|
150 | n/a | self.source_address = source_address |
---|
151 | n/a | self.sock = socket.create_connection((self.host, self.port), self.timeout, |
---|
152 | n/a | source_address=self.source_address) |
---|
153 | n/a | self.af = self.sock.family |
---|
154 | n/a | self.file = self.sock.makefile('r', encoding=self.encoding) |
---|
155 | n/a | self.welcome = self.getresp() |
---|
156 | n/a | return self.welcome |
---|
157 | n/a | |
---|
158 | n/a | def getwelcome(self): |
---|
159 | n/a | '''Get the welcome message from the server. |
---|
160 | n/a | (this is read and squirreled away by connect())''' |
---|
161 | n/a | if self.debugging: |
---|
162 | n/a | print('*welcome*', self.sanitize(self.welcome)) |
---|
163 | n/a | return self.welcome |
---|
164 | n/a | |
---|
165 | n/a | def set_debuglevel(self, level): |
---|
166 | n/a | '''Set the debugging level. |
---|
167 | n/a | The required argument level means: |
---|
168 | n/a | 0: no debugging output (default) |
---|
169 | n/a | 1: print commands and responses but not body text etc. |
---|
170 | n/a | 2: also print raw lines read and sent before stripping CR/LF''' |
---|
171 | n/a | self.debugging = level |
---|
172 | n/a | debug = set_debuglevel |
---|
173 | n/a | |
---|
174 | n/a | def set_pasv(self, val): |
---|
175 | n/a | '''Use passive or active mode for data transfers. |
---|
176 | n/a | With a false argument, use the normal PORT mode, |
---|
177 | n/a | With a true argument, use the PASV command.''' |
---|
178 | n/a | self.passiveserver = val |
---|
179 | n/a | |
---|
180 | n/a | # Internal: "sanitize" a string for printing |
---|
181 | n/a | def sanitize(self, s): |
---|
182 | n/a | if s[:5] in {'pass ', 'PASS '}: |
---|
183 | n/a | i = len(s.rstrip('\r\n')) |
---|
184 | n/a | s = s[:5] + '*'*(i-5) + s[i:] |
---|
185 | n/a | return repr(s) |
---|
186 | n/a | |
---|
187 | n/a | # Internal: send one line to the server, appending CRLF |
---|
188 | n/a | def putline(self, line): |
---|
189 | n/a | line = line + CRLF |
---|
190 | n/a | if self.debugging > 1: |
---|
191 | n/a | print('*put*', self.sanitize(line)) |
---|
192 | n/a | self.sock.sendall(line.encode(self.encoding)) |
---|
193 | n/a | |
---|
194 | n/a | # Internal: send one command to the server (through putline()) |
---|
195 | n/a | def putcmd(self, line): |
---|
196 | n/a | if self.debugging: print('*cmd*', self.sanitize(line)) |
---|
197 | n/a | self.putline(line) |
---|
198 | n/a | |
---|
199 | n/a | # Internal: return one line from the server, stripping CRLF. |
---|
200 | n/a | # Raise EOFError if the connection is closed |
---|
201 | n/a | def getline(self): |
---|
202 | n/a | line = self.file.readline(self.maxline + 1) |
---|
203 | n/a | if len(line) > self.maxline: |
---|
204 | n/a | raise Error("got more than %d bytes" % self.maxline) |
---|
205 | n/a | if self.debugging > 1: |
---|
206 | n/a | print('*get*', self.sanitize(line)) |
---|
207 | n/a | if not line: |
---|
208 | n/a | raise EOFError |
---|
209 | n/a | if line[-2:] == CRLF: |
---|
210 | n/a | line = line[:-2] |
---|
211 | n/a | elif line[-1:] in CRLF: |
---|
212 | n/a | line = line[:-1] |
---|
213 | n/a | return line |
---|
214 | n/a | |
---|
215 | n/a | # Internal: get a response from the server, which may possibly |
---|
216 | n/a | # consist of multiple lines. Return a single string with no |
---|
217 | n/a | # trailing CRLF. If the response consists of multiple lines, |
---|
218 | n/a | # these are separated by '\n' characters in the string |
---|
219 | n/a | def getmultiline(self): |
---|
220 | n/a | line = self.getline() |
---|
221 | n/a | if line[3:4] == '-': |
---|
222 | n/a | code = line[:3] |
---|
223 | n/a | while 1: |
---|
224 | n/a | nextline = self.getline() |
---|
225 | n/a | line = line + ('\n' + nextline) |
---|
226 | n/a | if nextline[:3] == code and \ |
---|
227 | n/a | nextline[3:4] != '-': |
---|
228 | n/a | break |
---|
229 | n/a | return line |
---|
230 | n/a | |
---|
231 | n/a | # Internal: get a response from the server. |
---|
232 | n/a | # Raise various errors if the response indicates an error |
---|
233 | n/a | def getresp(self): |
---|
234 | n/a | resp = self.getmultiline() |
---|
235 | n/a | if self.debugging: |
---|
236 | n/a | print('*resp*', self.sanitize(resp)) |
---|
237 | n/a | self.lastresp = resp[:3] |
---|
238 | n/a | c = resp[:1] |
---|
239 | n/a | if c in {'1', '2', '3'}: |
---|
240 | n/a | return resp |
---|
241 | n/a | if c == '4': |
---|
242 | n/a | raise error_temp(resp) |
---|
243 | n/a | if c == '5': |
---|
244 | n/a | raise error_perm(resp) |
---|
245 | n/a | raise error_proto(resp) |
---|
246 | n/a | |
---|
247 | n/a | def voidresp(self): |
---|
248 | n/a | """Expect a response beginning with '2'.""" |
---|
249 | n/a | resp = self.getresp() |
---|
250 | n/a | if resp[:1] != '2': |
---|
251 | n/a | raise error_reply(resp) |
---|
252 | n/a | return resp |
---|
253 | n/a | |
---|
254 | n/a | def abort(self): |
---|
255 | n/a | '''Abort a file transfer. Uses out-of-band data. |
---|
256 | n/a | This does not follow the procedure from the RFC to send Telnet |
---|
257 | n/a | IP and Synch; that doesn't seem to work with the servers I've |
---|
258 | n/a | tried. Instead, just send the ABOR command as OOB data.''' |
---|
259 | n/a | line = b'ABOR' + B_CRLF |
---|
260 | n/a | if self.debugging > 1: |
---|
261 | n/a | print('*put urgent*', self.sanitize(line)) |
---|
262 | n/a | self.sock.sendall(line, MSG_OOB) |
---|
263 | n/a | resp = self.getmultiline() |
---|
264 | n/a | if resp[:3] not in {'426', '225', '226'}: |
---|
265 | n/a | raise error_proto(resp) |
---|
266 | n/a | return resp |
---|
267 | n/a | |
---|
268 | n/a | def sendcmd(self, cmd): |
---|
269 | n/a | '''Send a command and return the response.''' |
---|
270 | n/a | self.putcmd(cmd) |
---|
271 | n/a | return self.getresp() |
---|
272 | n/a | |
---|
273 | n/a | def voidcmd(self, cmd): |
---|
274 | n/a | """Send a command and expect a response beginning with '2'.""" |
---|
275 | n/a | self.putcmd(cmd) |
---|
276 | n/a | return self.voidresp() |
---|
277 | n/a | |
---|
278 | n/a | def sendport(self, host, port): |
---|
279 | n/a | '''Send a PORT command with the current host and the given |
---|
280 | n/a | port number. |
---|
281 | n/a | ''' |
---|
282 | n/a | hbytes = host.split('.') |
---|
283 | n/a | pbytes = [repr(port//256), repr(port%256)] |
---|
284 | n/a | bytes = hbytes + pbytes |
---|
285 | n/a | cmd = 'PORT ' + ','.join(bytes) |
---|
286 | n/a | return self.voidcmd(cmd) |
---|
287 | n/a | |
---|
288 | n/a | def sendeprt(self, host, port): |
---|
289 | n/a | '''Send an EPRT command with the current host and the given port number.''' |
---|
290 | n/a | af = 0 |
---|
291 | n/a | if self.af == socket.AF_INET: |
---|
292 | n/a | af = 1 |
---|
293 | n/a | if self.af == socket.AF_INET6: |
---|
294 | n/a | af = 2 |
---|
295 | n/a | if af == 0: |
---|
296 | n/a | raise error_proto('unsupported address family') |
---|
297 | n/a | fields = ['', repr(af), host, repr(port), ''] |
---|
298 | n/a | cmd = 'EPRT ' + '|'.join(fields) |
---|
299 | n/a | return self.voidcmd(cmd) |
---|
300 | n/a | |
---|
301 | n/a | def makeport(self): |
---|
302 | n/a | '''Create a new socket and send a PORT command for it.''' |
---|
303 | n/a | err = None |
---|
304 | n/a | sock = None |
---|
305 | n/a | for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): |
---|
306 | n/a | af, socktype, proto, canonname, sa = res |
---|
307 | n/a | try: |
---|
308 | n/a | sock = socket.socket(af, socktype, proto) |
---|
309 | n/a | sock.bind(sa) |
---|
310 | n/a | except OSError as _: |
---|
311 | n/a | err = _ |
---|
312 | n/a | if sock: |
---|
313 | n/a | sock.close() |
---|
314 | n/a | sock = None |
---|
315 | n/a | continue |
---|
316 | n/a | break |
---|
317 | n/a | if sock is None: |
---|
318 | n/a | if err is not None: |
---|
319 | n/a | raise err |
---|
320 | n/a | else: |
---|
321 | n/a | raise OSError("getaddrinfo returns an empty list") |
---|
322 | n/a | sock.listen(1) |
---|
323 | n/a | port = sock.getsockname()[1] # Get proper port |
---|
324 | n/a | host = self.sock.getsockname()[0] # Get proper host |
---|
325 | n/a | if self.af == socket.AF_INET: |
---|
326 | n/a | resp = self.sendport(host, port) |
---|
327 | n/a | else: |
---|
328 | n/a | resp = self.sendeprt(host, port) |
---|
329 | n/a | if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT: |
---|
330 | n/a | sock.settimeout(self.timeout) |
---|
331 | n/a | return sock |
---|
332 | n/a | |
---|
333 | n/a | def makepasv(self): |
---|
334 | n/a | if self.af == socket.AF_INET: |
---|
335 | n/a | host, port = parse227(self.sendcmd('PASV')) |
---|
336 | n/a | else: |
---|
337 | n/a | host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) |
---|
338 | n/a | return host, port |
---|
339 | n/a | |
---|
340 | n/a | def ntransfercmd(self, cmd, rest=None): |
---|
341 | n/a | """Initiate a transfer over the data connection. |
---|
342 | n/a | |
---|
343 | n/a | If the transfer is active, send a port command and the |
---|
344 | n/a | transfer command, and accept the connection. If the server is |
---|
345 | n/a | passive, send a pasv command, connect to it, and start the |
---|
346 | n/a | transfer command. Either way, return the socket for the |
---|
347 | n/a | connection and the expected size of the transfer. The |
---|
348 | n/a | expected size may be None if it could not be determined. |
---|
349 | n/a | |
---|
350 | n/a | Optional `rest' argument can be a string that is sent as the |
---|
351 | n/a | argument to a REST command. This is essentially a server |
---|
352 | n/a | marker used to tell the server to skip over any data up to the |
---|
353 | n/a | given marker. |
---|
354 | n/a | """ |
---|
355 | n/a | size = None |
---|
356 | n/a | if self.passiveserver: |
---|
357 | n/a | host, port = self.makepasv() |
---|
358 | n/a | conn = socket.create_connection((host, port), self.timeout, |
---|
359 | n/a | source_address=self.source_address) |
---|
360 | n/a | try: |
---|
361 | n/a | if rest is not None: |
---|
362 | n/a | self.sendcmd("REST %s" % rest) |
---|
363 | n/a | resp = self.sendcmd(cmd) |
---|
364 | n/a | # Some servers apparently send a 200 reply to |
---|
365 | n/a | # a LIST or STOR command, before the 150 reply |
---|
366 | n/a | # (and way before the 226 reply). This seems to |
---|
367 | n/a | # be in violation of the protocol (which only allows |
---|
368 | n/a | # 1xx or error messages for LIST), so we just discard |
---|
369 | n/a | # this response. |
---|
370 | n/a | if resp[0] == '2': |
---|
371 | n/a | resp = self.getresp() |
---|
372 | n/a | if resp[0] != '1': |
---|
373 | n/a | raise error_reply(resp) |
---|
374 | n/a | except: |
---|
375 | n/a | conn.close() |
---|
376 | n/a | raise |
---|
377 | n/a | else: |
---|
378 | n/a | with self.makeport() as sock: |
---|
379 | n/a | if rest is not None: |
---|
380 | n/a | self.sendcmd("REST %s" % rest) |
---|
381 | n/a | resp = self.sendcmd(cmd) |
---|
382 | n/a | # See above. |
---|
383 | n/a | if resp[0] == '2': |
---|
384 | n/a | resp = self.getresp() |
---|
385 | n/a | if resp[0] != '1': |
---|
386 | n/a | raise error_reply(resp) |
---|
387 | n/a | conn, sockaddr = sock.accept() |
---|
388 | n/a | if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT: |
---|
389 | n/a | conn.settimeout(self.timeout) |
---|
390 | n/a | if resp[:3] == '150': |
---|
391 | n/a | # this is conditional in case we received a 125 |
---|
392 | n/a | size = parse150(resp) |
---|
393 | n/a | return conn, size |
---|
394 | n/a | |
---|
395 | n/a | def transfercmd(self, cmd, rest=None): |
---|
396 | n/a | """Like ntransfercmd() but returns only the socket.""" |
---|
397 | n/a | return self.ntransfercmd(cmd, rest)[0] |
---|
398 | n/a | |
---|
399 | n/a | def login(self, user = '', passwd = '', acct = ''): |
---|
400 | n/a | '''Login, default anonymous.''' |
---|
401 | n/a | if not user: |
---|
402 | n/a | user = 'anonymous' |
---|
403 | n/a | if not passwd: |
---|
404 | n/a | passwd = '' |
---|
405 | n/a | if not acct: |
---|
406 | n/a | acct = '' |
---|
407 | n/a | if user == 'anonymous' and passwd in {'', '-'}: |
---|
408 | n/a | # If there is no anonymous ftp password specified |
---|
409 | n/a | # then we'll just use anonymous@ |
---|
410 | n/a | # We don't send any other thing because: |
---|
411 | n/a | # - We want to remain anonymous |
---|
412 | n/a | # - We want to stop SPAM |
---|
413 | n/a | # - We don't want to let ftp sites to discriminate by the user, |
---|
414 | n/a | # host or country. |
---|
415 | n/a | passwd = passwd + 'anonymous@' |
---|
416 | n/a | resp = self.sendcmd('USER ' + user) |
---|
417 | n/a | if resp[0] == '3': |
---|
418 | n/a | resp = self.sendcmd('PASS ' + passwd) |
---|
419 | n/a | if resp[0] == '3': |
---|
420 | n/a | resp = self.sendcmd('ACCT ' + acct) |
---|
421 | n/a | if resp[0] != '2': |
---|
422 | n/a | raise error_reply(resp) |
---|
423 | n/a | return resp |
---|
424 | n/a | |
---|
425 | n/a | def retrbinary(self, cmd, callback, blocksize=8192, rest=None): |
---|
426 | n/a | """Retrieve data in binary mode. A new port is created for you. |
---|
427 | n/a | |
---|
428 | n/a | Args: |
---|
429 | n/a | cmd: A RETR command. |
---|
430 | n/a | callback: A single parameter callable to be called on each |
---|
431 | n/a | block of data read. |
---|
432 | n/a | blocksize: The maximum number of bytes to read from the |
---|
433 | n/a | socket at one time. [default: 8192] |
---|
434 | n/a | rest: Passed to transfercmd(). [default: None] |
---|
435 | n/a | |
---|
436 | n/a | Returns: |
---|
437 | n/a | The response code. |
---|
438 | n/a | """ |
---|
439 | n/a | self.voidcmd('TYPE I') |
---|
440 | n/a | with self.transfercmd(cmd, rest) as conn: |
---|
441 | n/a | while 1: |
---|
442 | n/a | data = conn.recv(blocksize) |
---|
443 | n/a | if not data: |
---|
444 | n/a | break |
---|
445 | n/a | callback(data) |
---|
446 | n/a | # shutdown ssl layer |
---|
447 | n/a | if _SSLSocket is not None and isinstance(conn, _SSLSocket): |
---|
448 | n/a | conn.unwrap() |
---|
449 | n/a | return self.voidresp() |
---|
450 | n/a | |
---|
451 | n/a | def retrlines(self, cmd, callback = None): |
---|
452 | n/a | """Retrieve data in line mode. A new port is created for you. |
---|
453 | n/a | |
---|
454 | n/a | Args: |
---|
455 | n/a | cmd: A RETR, LIST, or NLST command. |
---|
456 | n/a | callback: An optional single parameter callable that is called |
---|
457 | n/a | for each line with the trailing CRLF stripped. |
---|
458 | n/a | [default: print_line()] |
---|
459 | n/a | |
---|
460 | n/a | Returns: |
---|
461 | n/a | The response code. |
---|
462 | n/a | """ |
---|
463 | n/a | if callback is None: |
---|
464 | n/a | callback = print_line |
---|
465 | n/a | resp = self.sendcmd('TYPE A') |
---|
466 | n/a | with self.transfercmd(cmd) as conn, \ |
---|
467 | n/a | conn.makefile('r', encoding=self.encoding) as fp: |
---|
468 | n/a | while 1: |
---|
469 | n/a | line = fp.readline(self.maxline + 1) |
---|
470 | n/a | if len(line) > self.maxline: |
---|
471 | n/a | raise Error("got more than %d bytes" % self.maxline) |
---|
472 | n/a | if self.debugging > 2: |
---|
473 | n/a | print('*retr*', repr(line)) |
---|
474 | n/a | if not line: |
---|
475 | n/a | break |
---|
476 | n/a | if line[-2:] == CRLF: |
---|
477 | n/a | line = line[:-2] |
---|
478 | n/a | elif line[-1:] == '\n': |
---|
479 | n/a | line = line[:-1] |
---|
480 | n/a | callback(line) |
---|
481 | n/a | # shutdown ssl layer |
---|
482 | n/a | if _SSLSocket is not None and isinstance(conn, _SSLSocket): |
---|
483 | n/a | conn.unwrap() |
---|
484 | n/a | return self.voidresp() |
---|
485 | n/a | |
---|
486 | n/a | def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None): |
---|
487 | n/a | """Store a file in binary mode. A new port is created for you. |
---|
488 | n/a | |
---|
489 | n/a | Args: |
---|
490 | n/a | cmd: A STOR command. |
---|
491 | n/a | fp: A file-like object with a read(num_bytes) method. |
---|
492 | n/a | blocksize: The maximum data size to read from fp and send over |
---|
493 | n/a | the connection at once. [default: 8192] |
---|
494 | n/a | callback: An optional single parameter callable that is called on |
---|
495 | n/a | each block of data after it is sent. [default: None] |
---|
496 | n/a | rest: Passed to transfercmd(). [default: None] |
---|
497 | n/a | |
---|
498 | n/a | Returns: |
---|
499 | n/a | The response code. |
---|
500 | n/a | """ |
---|
501 | n/a | self.voidcmd('TYPE I') |
---|
502 | n/a | with self.transfercmd(cmd, rest) as conn: |
---|
503 | n/a | while 1: |
---|
504 | n/a | buf = fp.read(blocksize) |
---|
505 | n/a | if not buf: |
---|
506 | n/a | break |
---|
507 | n/a | conn.sendall(buf) |
---|
508 | n/a | if callback: |
---|
509 | n/a | callback(buf) |
---|
510 | n/a | # shutdown ssl layer |
---|
511 | n/a | if _SSLSocket is not None and isinstance(conn, _SSLSocket): |
---|
512 | n/a | conn.unwrap() |
---|
513 | n/a | return self.voidresp() |
---|
514 | n/a | |
---|
515 | n/a | def storlines(self, cmd, fp, callback=None): |
---|
516 | n/a | """Store a file in line mode. A new port is created for you. |
---|
517 | n/a | |
---|
518 | n/a | Args: |
---|
519 | n/a | cmd: A STOR command. |
---|
520 | n/a | fp: A file-like object with a readline() method. |
---|
521 | n/a | callback: An optional single parameter callable that is called on |
---|
522 | n/a | each line after it is sent. [default: None] |
---|
523 | n/a | |
---|
524 | n/a | Returns: |
---|
525 | n/a | The response code. |
---|
526 | n/a | """ |
---|
527 | n/a | self.voidcmd('TYPE A') |
---|
528 | n/a | with self.transfercmd(cmd) as conn: |
---|
529 | n/a | while 1: |
---|
530 | n/a | buf = fp.readline(self.maxline + 1) |
---|
531 | n/a | if len(buf) > self.maxline: |
---|
532 | n/a | raise Error("got more than %d bytes" % self.maxline) |
---|
533 | n/a | if not buf: |
---|
534 | n/a | break |
---|
535 | n/a | if buf[-2:] != B_CRLF: |
---|
536 | n/a | if buf[-1] in B_CRLF: buf = buf[:-1] |
---|
537 | n/a | buf = buf + B_CRLF |
---|
538 | n/a | conn.sendall(buf) |
---|
539 | n/a | if callback: |
---|
540 | n/a | callback(buf) |
---|
541 | n/a | # shutdown ssl layer |
---|
542 | n/a | if _SSLSocket is not None and isinstance(conn, _SSLSocket): |
---|
543 | n/a | conn.unwrap() |
---|
544 | n/a | return self.voidresp() |
---|
545 | n/a | |
---|
546 | n/a | def acct(self, password): |
---|
547 | n/a | '''Send new account name.''' |
---|
548 | n/a | cmd = 'ACCT ' + password |
---|
549 | n/a | return self.voidcmd(cmd) |
---|
550 | n/a | |
---|
551 | n/a | def nlst(self, *args): |
---|
552 | n/a | '''Return a list of files in a given directory (default the current).''' |
---|
553 | n/a | cmd = 'NLST' |
---|
554 | n/a | for arg in args: |
---|
555 | n/a | cmd = cmd + (' ' + arg) |
---|
556 | n/a | files = [] |
---|
557 | n/a | self.retrlines(cmd, files.append) |
---|
558 | n/a | return files |
---|
559 | n/a | |
---|
560 | n/a | def dir(self, *args): |
---|
561 | n/a | '''List a directory in long form. |
---|
562 | n/a | By default list current directory to stdout. |
---|
563 | n/a | Optional last argument is callback function; all |
---|
564 | n/a | non-empty arguments before it are concatenated to the |
---|
565 | n/a | LIST command. (This *should* only be used for a pathname.)''' |
---|
566 | n/a | cmd = 'LIST' |
---|
567 | n/a | func = None |
---|
568 | n/a | if args[-1:] and type(args[-1]) != type(''): |
---|
569 | n/a | args, func = args[:-1], args[-1] |
---|
570 | n/a | for arg in args: |
---|
571 | n/a | if arg: |
---|
572 | n/a | cmd = cmd + (' ' + arg) |
---|
573 | n/a | self.retrlines(cmd, func) |
---|
574 | n/a | |
---|
575 | n/a | def mlsd(self, path="", facts=[]): |
---|
576 | n/a | '''List a directory in a standardized format by using MLSD |
---|
577 | n/a | command (RFC-3659). If path is omitted the current directory |
---|
578 | n/a | is assumed. "facts" is a list of strings representing the type |
---|
579 | n/a | of information desired (e.g. ["type", "size", "perm"]). |
---|
580 | n/a | |
---|
581 | n/a | Return a generator object yielding a tuple of two elements |
---|
582 | n/a | for every file found in path. |
---|
583 | n/a | First element is the file name, the second one is a dictionary |
---|
584 | n/a | including a variable number of "facts" depending on the server |
---|
585 | n/a | and whether "facts" argument has been provided. |
---|
586 | n/a | ''' |
---|
587 | n/a | if facts: |
---|
588 | n/a | self.sendcmd("OPTS MLST " + ";".join(facts) + ";") |
---|
589 | n/a | if path: |
---|
590 | n/a | cmd = "MLSD %s" % path |
---|
591 | n/a | else: |
---|
592 | n/a | cmd = "MLSD" |
---|
593 | n/a | lines = [] |
---|
594 | n/a | self.retrlines(cmd, lines.append) |
---|
595 | n/a | for line in lines: |
---|
596 | n/a | facts_found, _, name = line.rstrip(CRLF).partition(' ') |
---|
597 | n/a | entry = {} |
---|
598 | n/a | for fact in facts_found[:-1].split(";"): |
---|
599 | n/a | key, _, value = fact.partition("=") |
---|
600 | n/a | entry[key.lower()] = value |
---|
601 | n/a | yield (name, entry) |
---|
602 | n/a | |
---|
603 | n/a | def rename(self, fromname, toname): |
---|
604 | n/a | '''Rename a file.''' |
---|
605 | n/a | resp = self.sendcmd('RNFR ' + fromname) |
---|
606 | n/a | if resp[0] != '3': |
---|
607 | n/a | raise error_reply(resp) |
---|
608 | n/a | return self.voidcmd('RNTO ' + toname) |
---|
609 | n/a | |
---|
610 | n/a | def delete(self, filename): |
---|
611 | n/a | '''Delete a file.''' |
---|
612 | n/a | resp = self.sendcmd('DELE ' + filename) |
---|
613 | n/a | if resp[:3] in {'250', '200'}: |
---|
614 | n/a | return resp |
---|
615 | n/a | else: |
---|
616 | n/a | raise error_reply(resp) |
---|
617 | n/a | |
---|
618 | n/a | def cwd(self, dirname): |
---|
619 | n/a | '''Change to a directory.''' |
---|
620 | n/a | if dirname == '..': |
---|
621 | n/a | try: |
---|
622 | n/a | return self.voidcmd('CDUP') |
---|
623 | n/a | except error_perm as msg: |
---|
624 | n/a | if msg.args[0][:3] != '500': |
---|
625 | n/a | raise |
---|
626 | n/a | elif dirname == '': |
---|
627 | n/a | dirname = '.' # does nothing, but could return error |
---|
628 | n/a | cmd = 'CWD ' + dirname |
---|
629 | n/a | return self.voidcmd(cmd) |
---|
630 | n/a | |
---|
631 | n/a | def size(self, filename): |
---|
632 | n/a | '''Retrieve the size of a file.''' |
---|
633 | n/a | # The SIZE command is defined in RFC-3659 |
---|
634 | n/a | resp = self.sendcmd('SIZE ' + filename) |
---|
635 | n/a | if resp[:3] == '213': |
---|
636 | n/a | s = resp[3:].strip() |
---|
637 | n/a | return int(s) |
---|
638 | n/a | |
---|
639 | n/a | def mkd(self, dirname): |
---|
640 | n/a | '''Make a directory, return its full pathname.''' |
---|
641 | n/a | resp = self.voidcmd('MKD ' + dirname) |
---|
642 | n/a | # fix around non-compliant implementations such as IIS shipped |
---|
643 | n/a | # with Windows server 2003 |
---|
644 | n/a | if not resp.startswith('257'): |
---|
645 | n/a | return '' |
---|
646 | n/a | return parse257(resp) |
---|
647 | n/a | |
---|
648 | n/a | def rmd(self, dirname): |
---|
649 | n/a | '''Remove a directory.''' |
---|
650 | n/a | return self.voidcmd('RMD ' + dirname) |
---|
651 | n/a | |
---|
652 | n/a | def pwd(self): |
---|
653 | n/a | '''Return current working directory.''' |
---|
654 | n/a | resp = self.voidcmd('PWD') |
---|
655 | n/a | # fix around non-compliant implementations such as IIS shipped |
---|
656 | n/a | # with Windows server 2003 |
---|
657 | n/a | if not resp.startswith('257'): |
---|
658 | n/a | return '' |
---|
659 | n/a | return parse257(resp) |
---|
660 | n/a | |
---|
661 | n/a | def quit(self): |
---|
662 | n/a | '''Quit, and close the connection.''' |
---|
663 | n/a | resp = self.voidcmd('QUIT') |
---|
664 | n/a | self.close() |
---|
665 | n/a | return resp |
---|
666 | n/a | |
---|
667 | n/a | def close(self): |
---|
668 | n/a | '''Close the connection without assuming anything about it.''' |
---|
669 | n/a | try: |
---|
670 | n/a | file = self.file |
---|
671 | n/a | self.file = None |
---|
672 | n/a | if file is not None: |
---|
673 | n/a | file.close() |
---|
674 | n/a | finally: |
---|
675 | n/a | sock = self.sock |
---|
676 | n/a | self.sock = None |
---|
677 | n/a | if sock is not None: |
---|
678 | n/a | sock.close() |
---|
679 | n/a | |
---|
680 | n/a | try: |
---|
681 | n/a | import ssl |
---|
682 | n/a | except ImportError: |
---|
683 | n/a | _SSLSocket = None |
---|
684 | n/a | else: |
---|
685 | n/a | _SSLSocket = ssl.SSLSocket |
---|
686 | n/a | |
---|
687 | n/a | class FTP_TLS(FTP): |
---|
688 | n/a | '''A FTP subclass which adds TLS support to FTP as described |
---|
689 | n/a | in RFC-4217. |
---|
690 | n/a | |
---|
691 | n/a | Connect as usual to port 21 implicitly securing the FTP control |
---|
692 | n/a | connection before authenticating. |
---|
693 | n/a | |
---|
694 | n/a | Securing the data connection requires user to explicitly ask |
---|
695 | n/a | for it by calling prot_p() method. |
---|
696 | n/a | |
---|
697 | n/a | Usage example: |
---|
698 | n/a | >>> from ftplib import FTP_TLS |
---|
699 | n/a | >>> ftps = FTP_TLS('ftp.python.org') |
---|
700 | n/a | >>> ftps.login() # login anonymously previously securing control channel |
---|
701 | n/a | '230 Guest login ok, access restrictions apply.' |
---|
702 | n/a | >>> ftps.prot_p() # switch to secure data connection |
---|
703 | n/a | '200 Protection level set to P' |
---|
704 | n/a | >>> ftps.retrlines('LIST') # list directory content securely |
---|
705 | n/a | total 9 |
---|
706 | n/a | drwxr-xr-x 8 root wheel 1024 Jan 3 1994 . |
---|
707 | n/a | drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .. |
---|
708 | n/a | drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin |
---|
709 | n/a | drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc |
---|
710 | n/a | d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming |
---|
711 | n/a | drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib |
---|
712 | n/a | drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub |
---|
713 | n/a | drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr |
---|
714 | n/a | -rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg |
---|
715 | n/a | '226 Transfer complete.' |
---|
716 | n/a | >>> ftps.quit() |
---|
717 | n/a | '221 Goodbye.' |
---|
718 | n/a | >>> |
---|
719 | n/a | ''' |
---|
720 | n/a | ssl_version = ssl.PROTOCOL_SSLv23 |
---|
721 | n/a | |
---|
722 | n/a | def __init__(self, host='', user='', passwd='', acct='', keyfile=None, |
---|
723 | n/a | certfile=None, context=None, |
---|
724 | n/a | timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None): |
---|
725 | n/a | if context is not None and keyfile is not None: |
---|
726 | n/a | raise ValueError("context and keyfile arguments are mutually " |
---|
727 | n/a | "exclusive") |
---|
728 | n/a | if context is not None and certfile is not None: |
---|
729 | n/a | raise ValueError("context and certfile arguments are mutually " |
---|
730 | n/a | "exclusive") |
---|
731 | n/a | if keyfile is not None or certfile is not None: |
---|
732 | n/a | import warnings |
---|
733 | n/a | warnings.warn("keyfile and certfile are deprecated, use a" |
---|
734 | n/a | "custom context instead", DeprecationWarning, 2) |
---|
735 | n/a | self.keyfile = keyfile |
---|
736 | n/a | self.certfile = certfile |
---|
737 | n/a | if context is None: |
---|
738 | n/a | context = ssl._create_stdlib_context(self.ssl_version, |
---|
739 | n/a | certfile=certfile, |
---|
740 | n/a | keyfile=keyfile) |
---|
741 | n/a | self.context = context |
---|
742 | n/a | self._prot_p = False |
---|
743 | n/a | FTP.__init__(self, host, user, passwd, acct, timeout, source_address) |
---|
744 | n/a | |
---|
745 | n/a | def login(self, user='', passwd='', acct='', secure=True): |
---|
746 | n/a | if secure and not isinstance(self.sock, ssl.SSLSocket): |
---|
747 | n/a | self.auth() |
---|
748 | n/a | return FTP.login(self, user, passwd, acct) |
---|
749 | n/a | |
---|
750 | n/a | def auth(self): |
---|
751 | n/a | '''Set up secure control connection by using TLS/SSL.''' |
---|
752 | n/a | if isinstance(self.sock, ssl.SSLSocket): |
---|
753 | n/a | raise ValueError("Already using TLS") |
---|
754 | n/a | if self.ssl_version >= ssl.PROTOCOL_SSLv23: |
---|
755 | n/a | resp = self.voidcmd('AUTH TLS') |
---|
756 | n/a | else: |
---|
757 | n/a | resp = self.voidcmd('AUTH SSL') |
---|
758 | n/a | self.sock = self.context.wrap_socket(self.sock, |
---|
759 | n/a | server_hostname=self.host) |
---|
760 | n/a | self.file = self.sock.makefile(mode='r', encoding=self.encoding) |
---|
761 | n/a | return resp |
---|
762 | n/a | |
---|
763 | n/a | def ccc(self): |
---|
764 | n/a | '''Switch back to a clear-text control connection.''' |
---|
765 | n/a | if not isinstance(self.sock, ssl.SSLSocket): |
---|
766 | n/a | raise ValueError("not using TLS") |
---|
767 | n/a | resp = self.voidcmd('CCC') |
---|
768 | n/a | self.sock = self.sock.unwrap() |
---|
769 | n/a | return resp |
---|
770 | n/a | |
---|
771 | n/a | def prot_p(self): |
---|
772 | n/a | '''Set up secure data connection.''' |
---|
773 | n/a | # PROT defines whether or not the data channel is to be protected. |
---|
774 | n/a | # Though RFC-2228 defines four possible protection levels, |
---|
775 | n/a | # RFC-4217 only recommends two, Clear and Private. |
---|
776 | n/a | # Clear (PROT C) means that no security is to be used on the |
---|
777 | n/a | # data-channel, Private (PROT P) means that the data-channel |
---|
778 | n/a | # should be protected by TLS. |
---|
779 | n/a | # PBSZ command MUST still be issued, but must have a parameter of |
---|
780 | n/a | # '0' to indicate that no buffering is taking place and the data |
---|
781 | n/a | # connection should not be encapsulated. |
---|
782 | n/a | self.voidcmd('PBSZ 0') |
---|
783 | n/a | resp = self.voidcmd('PROT P') |
---|
784 | n/a | self._prot_p = True |
---|
785 | n/a | return resp |
---|
786 | n/a | |
---|
787 | n/a | def prot_c(self): |
---|
788 | n/a | '''Set up clear text data connection.''' |
---|
789 | n/a | resp = self.voidcmd('PROT C') |
---|
790 | n/a | self._prot_p = False |
---|
791 | n/a | return resp |
---|
792 | n/a | |
---|
793 | n/a | # --- Overridden FTP methods |
---|
794 | n/a | |
---|
795 | n/a | def ntransfercmd(self, cmd, rest=None): |
---|
796 | n/a | conn, size = FTP.ntransfercmd(self, cmd, rest) |
---|
797 | n/a | if self._prot_p: |
---|
798 | n/a | conn = self.context.wrap_socket(conn, |
---|
799 | n/a | server_hostname=self.host) |
---|
800 | n/a | return conn, size |
---|
801 | n/a | |
---|
802 | n/a | def abort(self): |
---|
803 | n/a | # overridden as we can't pass MSG_OOB flag to sendall() |
---|
804 | n/a | line = b'ABOR' + B_CRLF |
---|
805 | n/a | self.sock.sendall(line) |
---|
806 | n/a | resp = self.getmultiline() |
---|
807 | n/a | if resp[:3] not in {'426', '225', '226'}: |
---|
808 | n/a | raise error_proto(resp) |
---|
809 | n/a | return resp |
---|
810 | n/a | |
---|
811 | n/a | __all__.append('FTP_TLS') |
---|
812 | n/a | all_errors = (Error, OSError, EOFError, ssl.SSLError) |
---|
813 | n/a | |
---|
814 | n/a | |
---|
815 | n/a | _150_re = None |
---|
816 | n/a | |
---|
817 | n/a | def parse150(resp): |
---|
818 | n/a | '''Parse the '150' response for a RETR request. |
---|
819 | n/a | Returns the expected transfer size or None; size is not guaranteed to |
---|
820 | n/a | be present in the 150 message. |
---|
821 | n/a | ''' |
---|
822 | n/a | if resp[:3] != '150': |
---|
823 | n/a | raise error_reply(resp) |
---|
824 | n/a | global _150_re |
---|
825 | n/a | if _150_re is None: |
---|
826 | n/a | import re |
---|
827 | n/a | _150_re = re.compile( |
---|
828 | n/a | r"150 .* \((\d+) bytes\)", re.IGNORECASE | re.ASCII) |
---|
829 | n/a | m = _150_re.match(resp) |
---|
830 | n/a | if not m: |
---|
831 | n/a | return None |
---|
832 | n/a | return int(m.group(1)) |
---|
833 | n/a | |
---|
834 | n/a | |
---|
835 | n/a | _227_re = None |
---|
836 | n/a | |
---|
837 | n/a | def parse227(resp): |
---|
838 | n/a | '''Parse the '227' response for a PASV request. |
---|
839 | n/a | Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)' |
---|
840 | n/a | Return ('host.addr.as.numbers', port#) tuple.''' |
---|
841 | n/a | |
---|
842 | n/a | if resp[:3] != '227': |
---|
843 | n/a | raise error_reply(resp) |
---|
844 | n/a | global _227_re |
---|
845 | n/a | if _227_re is None: |
---|
846 | n/a | import re |
---|
847 | n/a | _227_re = re.compile(r'(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)', re.ASCII) |
---|
848 | n/a | m = _227_re.search(resp) |
---|
849 | n/a | if not m: |
---|
850 | n/a | raise error_proto(resp) |
---|
851 | n/a | numbers = m.groups() |
---|
852 | n/a | host = '.'.join(numbers[:4]) |
---|
853 | n/a | port = (int(numbers[4]) << 8) + int(numbers[5]) |
---|
854 | n/a | return host, port |
---|
855 | n/a | |
---|
856 | n/a | |
---|
857 | n/a | def parse229(resp, peer): |
---|
858 | n/a | '''Parse the '229' response for an EPSV request. |
---|
859 | n/a | Raises error_proto if it does not contain '(|||port|)' |
---|
860 | n/a | Return ('host.addr.as.numbers', port#) tuple.''' |
---|
861 | n/a | |
---|
862 | n/a | if resp[:3] != '229': |
---|
863 | n/a | raise error_reply(resp) |
---|
864 | n/a | left = resp.find('(') |
---|
865 | n/a | if left < 0: raise error_proto(resp) |
---|
866 | n/a | right = resp.find(')', left + 1) |
---|
867 | n/a | if right < 0: |
---|
868 | n/a | raise error_proto(resp) # should contain '(|||port|)' |
---|
869 | n/a | if resp[left + 1] != resp[right - 1]: |
---|
870 | n/a | raise error_proto(resp) |
---|
871 | n/a | parts = resp[left + 1:right].split(resp[left+1]) |
---|
872 | n/a | if len(parts) != 5: |
---|
873 | n/a | raise error_proto(resp) |
---|
874 | n/a | host = peer[0] |
---|
875 | n/a | port = int(parts[3]) |
---|
876 | n/a | return host, port |
---|
877 | n/a | |
---|
878 | n/a | |
---|
879 | n/a | def parse257(resp): |
---|
880 | n/a | '''Parse the '257' response for a MKD or PWD request. |
---|
881 | n/a | This is a response to a MKD or PWD request: a directory name. |
---|
882 | n/a | Returns the directoryname in the 257 reply.''' |
---|
883 | n/a | |
---|
884 | n/a | if resp[:3] != '257': |
---|
885 | n/a | raise error_reply(resp) |
---|
886 | n/a | if resp[3:5] != ' "': |
---|
887 | n/a | return '' # Not compliant to RFC 959, but UNIX ftpd does this |
---|
888 | n/a | dirname = '' |
---|
889 | n/a | i = 5 |
---|
890 | n/a | n = len(resp) |
---|
891 | n/a | while i < n: |
---|
892 | n/a | c = resp[i] |
---|
893 | n/a | i = i+1 |
---|
894 | n/a | if c == '"': |
---|
895 | n/a | if i >= n or resp[i] != '"': |
---|
896 | n/a | break |
---|
897 | n/a | i = i+1 |
---|
898 | n/a | dirname = dirname + c |
---|
899 | n/a | return dirname |
---|
900 | n/a | |
---|
901 | n/a | |
---|
902 | n/a | def print_line(line): |
---|
903 | n/a | '''Default retrlines callback to print a line.''' |
---|
904 | n/a | print(line) |
---|
905 | n/a | |
---|
906 | n/a | |
---|
907 | n/a | def ftpcp(source, sourcename, target, targetname = '', type = 'I'): |
---|
908 | n/a | '''Copy file from one FTP-instance to another.''' |
---|
909 | n/a | if not targetname: |
---|
910 | n/a | targetname = sourcename |
---|
911 | n/a | type = 'TYPE ' + type |
---|
912 | n/a | source.voidcmd(type) |
---|
913 | n/a | target.voidcmd(type) |
---|
914 | n/a | sourcehost, sourceport = parse227(source.sendcmd('PASV')) |
---|
915 | n/a | target.sendport(sourcehost, sourceport) |
---|
916 | n/a | # RFC 959: the user must "listen" [...] BEFORE sending the |
---|
917 | n/a | # transfer request. |
---|
918 | n/a | # So: STOR before RETR, because here the target is a "user". |
---|
919 | n/a | treply = target.sendcmd('STOR ' + targetname) |
---|
920 | n/a | if treply[:3] not in {'125', '150'}: |
---|
921 | n/a | raise error_proto # RFC 959 |
---|
922 | n/a | sreply = source.sendcmd('RETR ' + sourcename) |
---|
923 | n/a | if sreply[:3] not in {'125', '150'}: |
---|
924 | n/a | raise error_proto # RFC 959 |
---|
925 | n/a | source.voidresp() |
---|
926 | n/a | target.voidresp() |
---|
927 | n/a | |
---|
928 | n/a | |
---|
929 | n/a | def test(): |
---|
930 | n/a | '''Test program. |
---|
931 | n/a | Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ... |
---|
932 | n/a | |
---|
933 | n/a | -d dir |
---|
934 | n/a | -l list |
---|
935 | n/a | -p password |
---|
936 | n/a | ''' |
---|
937 | n/a | |
---|
938 | n/a | if len(sys.argv) < 2: |
---|
939 | n/a | print(test.__doc__) |
---|
940 | n/a | sys.exit(0) |
---|
941 | n/a | |
---|
942 | n/a | import netrc |
---|
943 | n/a | |
---|
944 | n/a | debugging = 0 |
---|
945 | n/a | rcfile = None |
---|
946 | n/a | while sys.argv[1] == '-d': |
---|
947 | n/a | debugging = debugging+1 |
---|
948 | n/a | del sys.argv[1] |
---|
949 | n/a | if sys.argv[1][:2] == '-r': |
---|
950 | n/a | # get name of alternate ~/.netrc file: |
---|
951 | n/a | rcfile = sys.argv[1][2:] |
---|
952 | n/a | del sys.argv[1] |
---|
953 | n/a | host = sys.argv[1] |
---|
954 | n/a | ftp = FTP(host) |
---|
955 | n/a | ftp.set_debuglevel(debugging) |
---|
956 | n/a | userid = passwd = acct = '' |
---|
957 | n/a | try: |
---|
958 | n/a | netrcobj = netrc.netrc(rcfile) |
---|
959 | n/a | except OSError: |
---|
960 | n/a | if rcfile is not None: |
---|
961 | n/a | sys.stderr.write("Could not open account file" |
---|
962 | n/a | " -- using anonymous login.") |
---|
963 | n/a | else: |
---|
964 | n/a | try: |
---|
965 | n/a | userid, acct, passwd = netrcobj.authenticators(host) |
---|
966 | n/a | except KeyError: |
---|
967 | n/a | # no account for host |
---|
968 | n/a | sys.stderr.write( |
---|
969 | n/a | "No account -- using anonymous login.") |
---|
970 | n/a | ftp.login(userid, passwd, acct) |
---|
971 | n/a | for file in sys.argv[2:]: |
---|
972 | n/a | if file[:2] == '-l': |
---|
973 | n/a | ftp.dir(file[2:]) |
---|
974 | n/a | elif file[:2] == '-d': |
---|
975 | n/a | cmd = 'CWD' |
---|
976 | n/a | if file[2:]: cmd = cmd + ' ' + file[2:] |
---|
977 | n/a | resp = ftp.sendcmd(cmd) |
---|
978 | n/a | elif file == '-p': |
---|
979 | n/a | ftp.set_pasv(not ftp.passiveserver) |
---|
980 | n/a | else: |
---|
981 | n/a | ftp.retrbinary('RETR ' + file, \ |
---|
982 | n/a | sys.stdout.write, 1024) |
---|
983 | n/a | ftp.quit() |
---|
984 | n/a | |
---|
985 | n/a | |
---|
986 | n/a | if __name__ == '__main__': |
---|
987 | n/a | test() |
---|