Mercurial > cpython-lru_cache-weakref
annotate Lib/ftplib.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 4fb884bf147c |
| children | 040493de848d |
| rev | line source |
|---|---|
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
1 '''An FTP client class, and some helper functions. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
2 Based on RFC 959: File Transfer Protocol |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
3 (FTP), by J. Postel and J. Reynolds |
| 1085 | 4 |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
5 Changes and improvements suggested by Steve Majewski. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
6 Modified by Jack to work on the mac. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
7 Modified by Siebren to support docstrings and PASV. |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
8 |
| 1085 | 9 |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
10 Example: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
11 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
12 >>> from ftplib import FTP |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
13 >>> ftp = FTP('ftp.python.org') # connect to host, default port |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
14 >>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
15 >>> ftp.retrlines('LIST') # list directory contents |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
16 total 9 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
17 drwxr-xr-x 8 root wheel 1024 Jan 3 1994 . |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
18 drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
19 drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
20 drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
21 d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
22 drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
23 drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
24 drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
25 -rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
26 >>> ftp.quit() |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
27 >>> |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
28 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
29 A nice test that reveals some of the network dialogue would be: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
30 python ftplib.py -d localhost -l -p -l |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
31 ''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
32 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
33 |
| 1085 | 34 import os |
| 35 import sys | |
| 36 import string | |
| 37 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
38 # Import SOCKS module if it exists, else standard socket module socket |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
39 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
40 import SOCKS; socket = SOCKS |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
41 except ImportError: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
42 import socket |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
43 |
| 1085 | 44 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
45 # Magic number from <socket.h> |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
46 MSG_OOB = 0x1 # Process data out of band |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
47 |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
48 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
49 # The standard FTP server control port |
| 1085 | 50 FTP_PORT = 21 |
| 51 | |
| 52 | |
|
1089
8d62c9056fe1
Added all_errors, list of all possible exceptions.
Guido van Rossum <guido@python.org>
parents:
1088
diff
changeset
|
53 # Exception raised when an error or invalid response is received |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
54 error_reply = 'ftplib.error_reply' # unexpected [123]xx reply |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
55 error_temp = 'ftplib.error_temp' # 4xx errors |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
56 error_perm = 'ftplib.error_perm' # 5xx errors |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
57 error_proto = 'ftplib.error_proto' # response does not begin with [1-5] |
| 1085 | 58 |
| 59 | |
|
1089
8d62c9056fe1
Added all_errors, list of all possible exceptions.
Guido van Rossum <guido@python.org>
parents:
1088
diff
changeset
|
60 # All exceptions (hopefully) that may be raised here and that aren't |
|
8d62c9056fe1
Added all_errors, list of all possible exceptions.
Guido van Rossum <guido@python.org>
parents:
1088
diff
changeset
|
61 # (always) programming errors on our side |
|
8d62c9056fe1
Added all_errors, list of all possible exceptions.
Guido van Rossum <guido@python.org>
parents:
1088
diff
changeset
|
62 all_errors = (error_reply, error_temp, error_perm, error_proto, \ |
|
3132
70ee4e3cb3d8
actualized example, catch EOFError, print retrieved lines if debugging>2
Guido van Rossum <guido@python.org>
parents:
2902
diff
changeset
|
63 socket.error, IOError, EOFError) |
|
1089
8d62c9056fe1
Added all_errors, list of all possible exceptions.
Guido van Rossum <guido@python.org>
parents:
1088
diff
changeset
|
64 |
|
8d62c9056fe1
Added all_errors, list of all possible exceptions.
Guido van Rossum <guido@python.org>
parents:
1088
diff
changeset
|
65 |
| 1085 | 66 # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) |
| 67 CRLF = '\r\n' | |
| 68 | |
| 69 | |
| 70 # The class itself | |
| 71 class FTP: | |
| 72 | |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
73 '''An FTP client class. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
74 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
75 To create a connection, call the class using these argument: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
76 host, user, passwd, acct |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
77 These are all strings, and have default value ''. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
78 Then use self.connect() with optional host and port argument. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
79 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
80 To download a file, use ftp.retrlines('RETR ' + filename), |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
81 or ftp.retrbinary() with slightly different arguments. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
82 To upload a file, use ftp.storlines() or ftp.storbinary(), |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
83 which have an open file as argument (see their definitions |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
84 below for details). |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
85 The download/upload functions first issue appropriate TYPE |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
86 and PORT or PASV commands. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
87 ''' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
88 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
89 # Initialization method (called by class instantiation). |
|
1314
918bee90f73e
* calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents:
1297
diff
changeset
|
90 # Initialize host to localhost, port to standard ftp port |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
91 # Optional arguments are host (for connect()), |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
92 # and user, passwd, acct (for login()) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
93 def __init__(self, host = '', user = '', passwd = '', acct = ''): |
|
1314
918bee90f73e
* calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents:
1297
diff
changeset
|
94 # Initialize the instance to something mostly harmless |
| 1085 | 95 self.debugging = 0 |
|
1314
918bee90f73e
* calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents:
1297
diff
changeset
|
96 self.host = '' |
|
918bee90f73e
* calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents:
1297
diff
changeset
|
97 self.port = FTP_PORT |
|
918bee90f73e
* calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents:
1297
diff
changeset
|
98 self.sock = None |
|
918bee90f73e
* calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents:
1297
diff
changeset
|
99 self.file = None |
|
918bee90f73e
* calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents:
1297
diff
changeset
|
100 self.welcome = None |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
101 if host: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
102 self.connect(host) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
103 if user: self.login(user, passwd, acct) |
|
1314
918bee90f73e
* calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents:
1297
diff
changeset
|
104 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
105 def connect(self, host = '', port = 0): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
106 '''Connect to host. Arguments are: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
107 - host: hostname to connect to (string, default previous host) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
108 - port: port to connect to (integer, default previous port)''' |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
109 if host: self.host = host |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
110 if port: self.port = port |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
111 self.passiveserver = 0 |
| 1085 | 112 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 113 self.sock.connect(self.host, self.port) | |
|
4345
4fb884bf147c
Always open file objects in binary mode.
Guido van Rossum <guido@python.org>
parents:
3649
diff
changeset
|
114 self.file = self.sock.makefile('rb') |
| 1085 | 115 self.welcome = self.getresp() |
| 116 | |
| 117 def getwelcome(self): | |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
118 '''Get the welcome message from the server. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
119 (this is read and squirreled away by connect())''' |
|
2710
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
120 if self.debugging: |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
121 print '*welcome*', self.sanitize(self.welcome) |
| 1085 | 122 return self.welcome |
| 123 | |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
124 def set_debuglevel(self, level): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
125 '''Set the debugging level. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
126 The required argument level means: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
127 0: no debugging output (default) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
128 1: print commands and responses but not body text etc. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
129 2: also print raw lines read and sent before stripping CR/LF''' |
| 1085 | 130 self.debugging = level |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
131 debug = set_debuglevel |
| 1085 | 132 |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
133 def set_pasv(self, val): |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
134 '''Use passive or active mode for data transfers. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
135 With a false argument, use the normal PORT mode, |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
136 With a true argument, use the PASV command.''' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
137 self.passiveserver = val |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
138 |
|
2710
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
139 # Internal: "sanitize" a string for printing |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
140 def sanitize(self, s): |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
141 if s[:5] == 'pass ' or s[:5] == 'PASS ': |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
142 i = len(s) |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
143 while i > 5 and s[i-1] in '\r\n': |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
144 i = i-1 |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
145 s = s[:5] + '*'*(i-5) + s[i:] |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
146 return `s` |
|
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
147 |
| 1085 | 148 # Internal: send one line to the server, appending CRLF |
| 149 def putline(self, line): | |
| 150 line = line + CRLF | |
|
2710
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
151 if self.debugging > 1: print '*put*', self.sanitize(line) |
| 1085 | 152 self.sock.send(line) |
| 153 | |
| 154 # Internal: send one command to the server (through putline()) | |
| 155 def putcmd(self, line): | |
|
2710
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
156 if self.debugging: print '*cmd*', self.sanitize(line) |
| 1085 | 157 self.putline(line) |
| 158 | |
| 159 # Internal: return one line from the server, stripping CRLF. | |
| 160 # Raise EOFError if the connection is closed | |
| 161 def getline(self): | |
| 162 line = self.file.readline() | |
| 163 if self.debugging > 1: | |
|
2710
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
164 print '*get*', self.sanitize(line) |
| 1085 | 165 if not line: raise EOFError |
| 166 if line[-2:] == CRLF: line = line[:-2] | |
| 167 elif line[-1:] in CRLF: line = line[:-1] | |
| 168 return line | |
| 169 | |
| 170 # Internal: get a response from the server, which may possibly | |
| 171 # consist of multiple lines. Return a single string with no | |
| 172 # trailing CRLF. If the response consists of multiple lines, | |
| 173 # these are separated by '\n' characters in the string | |
| 174 def getmultiline(self): | |
| 175 line = self.getline() | |
| 176 if line[3:4] == '-': | |
| 177 code = line[:3] | |
| 178 while 1: | |
| 179 nextline = self.getline() | |
| 180 line = line + ('\n' + nextline) | |
| 181 if nextline[:3] == code and \ | |
| 182 nextline[3:4] <> '-': | |
| 183 break | |
| 184 return line | |
| 185 | |
| 186 # Internal: get a response from the server. | |
| 187 # Raise various errors if the response indicates an error | |
| 188 def getresp(self): | |
| 189 resp = self.getmultiline() | |
|
2710
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
190 if self.debugging: print '*resp*', self.sanitize(resp) |
| 1085 | 191 self.lastresp = resp[:3] |
| 192 c = resp[:1] | |
| 193 if c == '4': | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
194 raise error_temp, resp |
| 1085 | 195 if c == '5': |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
196 raise error_perm, resp |
| 1085 | 197 if c not in '123': |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
198 raise error_proto, resp |
| 1085 | 199 return resp |
| 200 | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
201 def voidresp(self): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
202 """Expect a response beginning with '2'.""" |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
203 resp = self.getresp() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
204 if resp[0] <> '2': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
205 raise error_reply, resp |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
206 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
207 def abort(self): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
208 '''Abort a file transfer. Uses out-of-band data. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
209 This does not follow the procedure from the RFC to send Telnet |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
210 IP and Synch; that doesn't seem to work with the servers I've |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
211 tried. Instead, just send the ABOR command as OOB data.''' |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
212 line = 'ABOR' + CRLF |
|
2710
b689e6f92f17
don't show print passwords in debug output
Guido van Rossum <guido@python.org>
parents:
2708
diff
changeset
|
213 if self.debugging > 1: print '*put urgent*', self.sanitize(line) |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
214 self.sock.send(line, MSG_OOB) |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
215 resp = self.getmultiline() |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
216 if resp[:3] not in ('426', '226'): |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
217 raise error_proto, resp |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1286
diff
changeset
|
218 |
| 1085 | 219 def sendcmd(self, cmd): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
220 '''Send a command and return the response.''' |
| 1085 | 221 self.putcmd(cmd) |
| 222 return self.getresp() | |
| 223 | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
224 def voidcmd(self, cmd): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
225 """Send a command and expect a response beginning with '2'.""" |
|
1088
35824899aacf
Use getsockname() if it exists
Guido van Rossum <guido@python.org>
parents:
1087
diff
changeset
|
226 self.putcmd(cmd) |
|
35824899aacf
Use getsockname() if it exists
Guido van Rossum <guido@python.org>
parents:
1087
diff
changeset
|
227 self.voidresp() |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
228 |
|
2902
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
229 def sendport(self, host, port): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
230 '''Send a PORT command with the current host and the given port number.''' |
|
2902
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
231 hbytes = string.splitfields(host, '.') |
| 1085 | 232 pbytes = [`port/256`, `port%256`] |
| 233 bytes = hbytes + pbytes | |
| 234 cmd = 'PORT ' + string.joinfields(bytes, ',') | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
235 self.voidcmd(cmd) |
| 1085 | 236 |
| 237 def makeport(self): | |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
238 '''Create a new socket and send a PORT command for it.''' |
| 1085 | 239 global nextport |
| 240 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
|
2758
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
241 sock.bind(('', 0)) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
242 sock.listen(1) |
|
2902
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
243 dummyhost, port = sock.getsockname() # Get proper port |
|
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
244 host, dummyport = self.sock.getsockname() # Get proper host |
|
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
245 resp = self.sendport(host, port) |
| 1085 | 246 return sock |
| 247 | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
248 def transfercmd(self, cmd): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
249 '''Initiate a transfer over the data connection. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
250 If the transfer is active, send a port command and |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
251 the transfer command, and accept the connection. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
252 If the server is passive, send a pasv command, connect |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
253 to it, and start the transfer command. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
254 Either way, return the socket for the connection''' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
255 if self.passiveserver: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
256 host, port = parse227(self.sendcmd('PASV')) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
257 conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
258 conn.connect(host, port) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
259 resp = self.sendcmd(cmd) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
260 if resp[0] <> '1': |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
261 raise error_reply, resp |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
262 else: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
263 sock = self.makeport() |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
264 resp = self.sendcmd(cmd) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
265 if resp[0] <> '1': |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
266 raise error_reply, resp |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
267 conn, sockaddr = sock.accept() |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
268 return conn |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
269 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
270 def login(self, user = '', passwd = '', acct = ''): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
271 '''Login, default anonymous.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
272 if not user: user = 'anonymous' |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
273 if user == 'anonymous' and passwd in ('', '-'): |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
274 thishost = socket.gethostname() |
|
2708
373c1637d301
For anonymous ftp, make sure local hostname is fully qualified.
Jack Jansen <jack.jansen@cwi.nl>
parents:
2192
diff
changeset
|
275 # Make sure it is fully qualified |
|
373c1637d301
For anonymous ftp, make sure local hostname is fully qualified.
Jack Jansen <jack.jansen@cwi.nl>
parents:
2192
diff
changeset
|
276 if not '.' in thishost: |
|
373c1637d301
For anonymous ftp, make sure local hostname is fully qualified.
Jack Jansen <jack.jansen@cwi.nl>
parents:
2192
diff
changeset
|
277 thisaddr = socket.gethostbyname(thishost) |
|
2758
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
278 firstname, names, unused = \ |
|
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
279 socket.gethostbyaddr(thisaddr) |
|
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
280 names.insert(0, firstname) |
|
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
281 for name in names: |
|
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
282 if '.' in name: |
|
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
283 thishost = name |
|
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
284 break |
|
2192
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
285 try: |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
286 if os.environ.has_key('LOGNAME'): |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
287 realuser = os.environ['LOGNAME'] |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
288 elif os.environ.has_key('USER'): |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
289 realuser = os.environ['USER'] |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
290 else: |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
291 realuser = 'anonymous' |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
292 except AttributeError: |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
293 # Not all systems have os.environ.... |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
294 realuser = 'anonymous' |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
295 passwd = passwd + realuser + '@' + thishost |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
296 resp = self.sendcmd('USER ' + user) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
297 if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
298 if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
299 if resp[0] <> '2': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
300 raise error_reply, resp |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
301 |
| 1085 | 302 def retrbinary(self, cmd, callback, blocksize): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
303 '''Retrieve data in binary mode. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
304 The argument is a RETR command. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
305 The callback function is called for each block. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
306 This creates a new port for you''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
307 self.voidcmd('TYPE I') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
308 conn = self.transfercmd(cmd) |
| 1085 | 309 while 1: |
| 310 data = conn.recv(blocksize) | |
| 311 if not data: | |
| 312 break | |
| 313 callback(data) | |
| 314 conn.close() | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
315 self.voidresp() |
| 1085 | 316 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
317 def retrlines(self, cmd, callback = None): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
318 '''Retrieve data in line mode. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
319 The argument is a RETR or LIST command. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
320 The callback function (2nd argument) is called for each line, |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
321 with trailing CRLF stripped. This creates a new port for you. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
322 print_lines is the default callback.''' |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
323 if not callback: callback = print_line |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
324 resp = self.sendcmd('TYPE A') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
325 conn = self.transfercmd(cmd) |
|
4345
4fb884bf147c
Always open file objects in binary mode.
Guido van Rossum <guido@python.org>
parents:
3649
diff
changeset
|
326 fp = conn.makefile('rb') |
| 1085 | 327 while 1: |
| 328 line = fp.readline() | |
|
3132
70ee4e3cb3d8
actualized example, catch EOFError, print retrieved lines if debugging>2
Guido van Rossum <guido@python.org>
parents:
2902
diff
changeset
|
329 if self.debugging > 2: print '*retr*', `line` |
| 1085 | 330 if not line: |
| 331 break | |
| 332 if line[-2:] == CRLF: | |
| 333 line = line[:-2] | |
| 334 elif line[:-1] == '\n': | |
| 335 line = line[:-1] | |
| 336 callback(line) | |
| 337 fp.close() | |
| 338 conn.close() | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
339 self.voidresp() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
340 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
341 def storbinary(self, cmd, fp, blocksize): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
342 '''Store a file in binary mode.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
343 self.voidcmd('TYPE I') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
344 conn = self.transfercmd(cmd) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
345 while 1: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
346 buf = fp.read(blocksize) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
347 if not buf: break |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
348 conn.send(buf) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
349 conn.close() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
350 self.voidresp() |
| 1085 | 351 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
352 def storlines(self, cmd, fp): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
353 '''Store a file in line mode.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
354 self.voidcmd('TYPE A') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
355 conn = self.transfercmd(cmd) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
356 while 1: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
357 buf = fp.readline() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
358 if not buf: break |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
359 if buf[-2:] <> CRLF: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
360 if buf[-1] in CRLF: buf = buf[:-1] |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
361 buf = buf + CRLF |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
362 conn.send(buf) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
363 conn.close() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
364 self.voidresp() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
365 |
|
3359
08985431500b
add acct() method to send new account name
Guido van Rossum <guido@python.org>
parents:
3201
diff
changeset
|
366 def acct(self, password): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
367 '''Send new account name.''' |
|
3359
08985431500b
add acct() method to send new account name
Guido van Rossum <guido@python.org>
parents:
3201
diff
changeset
|
368 cmd = 'ACCT ' + password |
|
08985431500b
add acct() method to send new account name
Guido van Rossum <guido@python.org>
parents:
3201
diff
changeset
|
369 self.voidcmd(cmd) |
|
08985431500b
add acct() method to send new account name
Guido van Rossum <guido@python.org>
parents:
3201
diff
changeset
|
370 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
371 def nlst(self, *args): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
372 '''Return a list of files in a given directory (default the current).''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
373 cmd = 'NLST' |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
374 for arg in args: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
375 cmd = cmd + (' ' + arg) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
376 files = [] |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
377 self.retrlines(cmd, files.append) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
378 return files |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
379 |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
380 def dir(self, *args): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
381 '''List a directory in long form. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
382 By default list current directory to stdout. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
383 Optional last argument is callback function; all |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
384 non-empty arguments before it are concatenated to the |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
385 LIST command. (This *should* only be used for a pathname.)''' |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
386 cmd = 'LIST' |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
387 func = None |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
388 if args[-1:] and type(args[-1]) != type(''): |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
389 args, func = args[:-1], args[-1] |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
390 for arg in args: |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
391 if arg: |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
392 cmd = cmd + (' ' + arg) |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
393 self.retrlines(cmd, func) |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
394 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
395 def rename(self, fromname, toname): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
396 '''Rename a file.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
397 resp = self.sendcmd('RNFR ' + fromname) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
398 if resp[0] <> '3': |
| 1085 | 399 raise error_reply, resp |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
400 self.voidcmd('RNTO ' + toname) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
401 |
|
3201
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
402 def delete(self, filename): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
403 '''Delete a file.''' |
|
3201
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
404 resp = self.sendcmd('DELE ' + filename) |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
405 if resp[:3] == '250': |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
406 return |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
407 elif resp[:1] == '5': |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
408 raise error_perm, resp |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
409 else: |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
410 raise error_reply, resp |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
411 |
| 1286 | 412 def cwd(self, dirname): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
413 '''Change to a directory.''' |
|
1320
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1314
diff
changeset
|
414 if dirname == '..': |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
415 try: |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
416 self.voidcmd('CDUP') |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
417 return |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
418 except error_perm, msg: |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
419 if msg[:3] != '500': |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
420 raise error_perm, msg |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
421 cmd = 'CWD ' + dirname |
|
1320
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1314
diff
changeset
|
422 self.voidcmd(cmd) |
| 1286 | 423 |
|
1297
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
424 def size(self, filename): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
425 '''Retrieve the size of a file.''' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
426 # Note that the RFC doesn't say anything about 'SIZE' |
|
1297
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
427 resp = self.sendcmd('SIZE ' + filename) |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
428 if resp[:3] == '213': |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
429 return string.atoi(string.strip(resp[3:])) |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
430 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
431 def mkd(self, dirname): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
432 '''Make a directory, return its full pathname.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
433 resp = self.sendcmd('MKD ' + dirname) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
434 return parse257(resp) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
435 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
436 def pwd(self): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
437 '''Return current working directory.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
438 resp = self.sendcmd('PWD') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
439 return parse257(resp) |
| 1085 | 440 |
| 441 def quit(self): | |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
442 '''Quit, and close the connection.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
443 self.voidcmd('QUIT') |
|
1297
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
444 self.close() |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
445 |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
446 def close(self): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
447 '''Close the connection without assuming anything about it.''' |
| 1085 | 448 self.file.close() |
| 449 self.sock.close() | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
450 del self.file, self.sock |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
451 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
452 |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
453 def parse227(resp): |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
454 '''Parse the '227' response for a PASV request. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
455 Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
456 Return ('host.addr.as.numbers', port#) tuple.''' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
457 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
458 if resp[:3] <> '227': |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
459 raise error_reply, resp |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
460 left = string.find(resp, '(') |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
461 if left < 0: raise error_proto, resp |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
462 right = string.find(resp, ')', left + 1) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
463 if right < 0: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
464 raise error_proto, resp # should contain '(h1,h2,h3,h4,p1,p2)' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
465 numbers = string.split(resp[left+1:right], ',') |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
466 if len(numbers) <> 6: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
467 raise error_proto, resp |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
468 host = string.join(numbers[:4], '.') |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
469 port = (string.atoi(numbers[4]) << 8) + string.atoi(numbers[5]) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
470 return host, port |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
471 # end parse227 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
472 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
473 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
474 def parse257(resp): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
475 '''Parse the '257' response for a MKD or RMD request. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
476 This is a response to a MKD or RMD request: a directory name. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
477 Returns the directoryname in the 257 reply.''' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
478 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
479 if resp[:3] <> '257': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
480 raise error_reply, resp |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
481 if resp[3:5] <> ' "': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
482 return '' # Not compliant to RFC 959, but UNIX ftpd does this |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
483 dirname = '' |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
484 i = 5 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
485 n = len(resp) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
486 while i < n: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
487 c = resp[i] |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
488 i = i+1 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
489 if c == '"': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
490 if i >= n or resp[i] <> '"': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
491 break |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
492 i = i+1 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
493 dirname = dirname + c |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
494 return dirname |
| 1085 | 495 |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
496 def print_line(line): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
497 '''Default retrlines callback to print a line.''' |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
498 print line |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
499 |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
500 def ftpcp(source, sourcename, target, targetname = '', type = 'I'): |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
501 '''Copy file from one FTP-instance to another.''' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
502 if not targetname: targetname = sourcename |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
503 type = 'TYPE ' + type |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
504 source.voidcmd(type) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
505 target.voidcmd(type) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
506 sourcehost, sourceport = parse227(source.sendcmd('PASV')) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
507 target.sendport(sourcehost, sourceport) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
508 # RFC 959: the user must "listen" [...] BEFORE sending the |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
509 # transfer request. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
510 # So: STOR before RETR, because here the target is a "user". |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
511 treply = target.sendcmd('STOR ' + targetname) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
512 if treply[:3] not in ('125', '150'): raise error_proto # RFC 959 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
513 sreply = source.sendcmd('RETR ' + sourcename) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
514 if sreply[:3] not in ('125', '150'): raise error_proto # RFC 959 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
515 source.voidresp() |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
516 target.voidresp() |
| 1085 | 517 |
| 518 def test(): | |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
519 '''Test program. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
520 Usage: ftp [-d] host [-l[dir]] [-d[dir]] [-p] [file] ...''' |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
521 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
522 debugging = 0 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
523 while sys.argv[1] == '-d': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
524 debugging = debugging+1 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
525 del sys.argv[1] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
526 host = sys.argv[1] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
527 ftp = FTP(host) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
528 ftp.set_debuglevel(debugging) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
529 ftp.login() |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
530 for file in sys.argv[2:]: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
531 if file[:2] == '-l': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
532 ftp.dir(file[2:]) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
533 elif file[:2] == '-d': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
534 cmd = 'CWD' |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
535 if file[2:]: cmd = cmd + ' ' + file[2:] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
536 resp = ftp.sendcmd(cmd) |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
537 elif file == '-p': |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
538 ftp.set_pasv(not ftp.passiveserver) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
539 else: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
540 ftp.retrbinary('RETR ' + file, \ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
541 sys.stdout.write, 1024) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
542 ftp.quit() |
|
2902
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
543 |
|
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
544 |
|
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
545 if __name__ == '__main__': |
|
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
546 test() |
