Mercurial > cpython-lru_cache-weakref
annotate Lib/ftplib.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 64bc782a42ec |
| children | 068254fe5617 |
| 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 | |
|
4927
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
248 def ntransfercmd(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. |
|
4927
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
254 Either way, return the socket for the connection and |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
255 the expected size of the transfer. The expected size |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
256 may be None if it could not be determined.''' |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
257 size = None |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
258 if self.passiveserver: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 else: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
266 sock = self.makeport() |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
267 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
|
268 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
|
269 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
|
270 conn, sockaddr = sock.accept() |
|
4927
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
271 if resp[:3] == '150': |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
272 # this is conditional in case we received a 125 |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
273 size = parse150(resp) |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
274 return conn, size |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
275 |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
276 def transfercmd(self, cmd): |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
277 '''Initiate a transfer over the data connection. Returns |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
278 the socket for the connection. See also ntransfercmd().''' |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
279 return self.ntransfercmd(cmd)[0] |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
280 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
281 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
|
282 '''Login, default anonymous.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
283 if not user: user = 'anonymous' |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
284 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
|
285 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
|
286 # 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 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
|
292 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
|
293 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
|
294 thishost = name |
|
69aba6d9d378
add bind(0,''); better way of guessing our fully qualified hostname
Guido van Rossum <guido@python.org>
parents:
2710
diff
changeset
|
295 break |
|
2192
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
296 try: |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
297 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
|
298 realuser = os.environ['LOGNAME'] |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
299 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
|
300 realuser = os.environ['USER'] |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
301 else: |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
302 realuser = 'anonymous' |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
303 except AttributeError: |
|
f78fbccaf58c
Always use 'anonymous' if os.environ doesn't exist
Jack Jansen <jack.jansen@cwi.nl>
parents:
1746
diff
changeset
|
304 # 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
|
305 realuser = 'anonymous' |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
306 passwd = passwd + realuser + '@' + thishost |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
307 resp = self.sendcmd('USER ' + user) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
308 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
|
309 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
|
310 if resp[0] <> '2': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
311 raise error_reply, resp |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
312 |
| 1085 | 313 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
|
314 '''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
|
315 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
|
316 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
|
317 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
|
318 self.voidcmd('TYPE I') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
319 conn = self.transfercmd(cmd) |
| 1085 | 320 while 1: |
| 321 data = conn.recv(blocksize) | |
| 322 if not data: | |
| 323 break | |
| 324 callback(data) | |
| 325 conn.close() | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
326 self.voidresp() |
| 1085 | 327 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
328 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
|
329 '''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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 resp = self.sendcmd('TYPE A') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
336 conn = self.transfercmd(cmd) |
|
4345
4fb884bf147c
Always open file objects in binary mode.
Guido van Rossum <guido@python.org>
parents:
3649
diff
changeset
|
337 fp = conn.makefile('rb') |
| 1085 | 338 while 1: |
| 339 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
|
340 if self.debugging > 2: print '*retr*', `line` |
| 1085 | 341 if not line: |
| 342 break | |
| 343 if line[-2:] == CRLF: | |
| 344 line = line[:-2] | |
| 345 elif line[:-1] == '\n': | |
| 346 line = line[:-1] | |
| 347 callback(line) | |
| 348 fp.close() | |
| 349 conn.close() | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
350 self.voidresp() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
351 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
352 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
|
353 '''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
|
354 self.voidcmd('TYPE I') |
|
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.read(blocksize) |
|
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 conn.send(buf) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
360 conn.close() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
361 self.voidresp() |
| 1085 | 362 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
363 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
|
364 '''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
|
365 self.voidcmd('TYPE A') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
366 conn = self.transfercmd(cmd) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
367 while 1: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
368 buf = fp.readline() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
369 if not buf: break |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
370 if buf[-2:] <> CRLF: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
371 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
|
372 buf = buf + CRLF |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
373 conn.send(buf) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
374 conn.close() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
375 self.voidresp() |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
376 |
|
3359
08985431500b
add acct() method to send new account name
Guido van Rossum <guido@python.org>
parents:
3201
diff
changeset
|
377 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
|
378 '''Send new account name.''' |
|
3359
08985431500b
add acct() method to send new account name
Guido van Rossum <guido@python.org>
parents:
3201
diff
changeset
|
379 cmd = 'ACCT ' + password |
|
08985431500b
add acct() method to send new account name
Guido van Rossum <guido@python.org>
parents:
3201
diff
changeset
|
380 self.voidcmd(cmd) |
|
08985431500b
add acct() method to send new account name
Guido van Rossum <guido@python.org>
parents:
3201
diff
changeset
|
381 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
382 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
|
383 '''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
|
384 cmd = 'NLST' |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
385 for arg in args: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
386 cmd = cmd + (' ' + arg) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
387 files = [] |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
388 self.retrlines(cmd, files.append) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
389 return files |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
390 |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
391 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
|
392 '''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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 cmd = 'LIST' |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
398 func = None |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
399 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
|
400 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
|
401 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
|
402 if arg: |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
403 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
|
404 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
|
405 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
406 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
|
407 '''Rename a file.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
408 resp = self.sendcmd('RNFR ' + fromname) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
409 if resp[0] <> '3': |
| 1085 | 410 raise error_reply, resp |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
411 self.voidcmd('RNTO ' + toname) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
412 |
|
3201
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
413 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
|
414 '''Delete a file.''' |
|
3201
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
415 resp = self.sendcmd('DELE ' + filename) |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
416 if resp[:3] == '250': |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
417 return |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
418 elif resp[:1] == '5': |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
419 raise error_perm, resp |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
420 else: |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
421 raise error_reply, resp |
|
19b58ce794c9
add delete() method to ftp object
Guido van Rossum <guido@python.org>
parents:
3132
diff
changeset
|
422 |
| 1286 | 423 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
|
424 '''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
|
425 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
|
426 try: |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
427 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
|
428 return |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
429 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
|
430 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
|
431 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
|
432 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
|
433 self.voidcmd(cmd) |
| 1286 | 434 |
|
1297
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
435 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
|
436 '''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
|
437 # 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
|
438 resp = self.sendcmd('SIZE ' + filename) |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
439 if resp[:3] == '213': |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
440 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
|
441 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
442 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
|
443 '''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
|
444 resp = self.sendcmd('MKD ' + dirname) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
445 return parse257(resp) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
446 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
447 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
|
448 '''Return current working directory.''' |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
449 resp = self.sendcmd('PWD') |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
450 return parse257(resp) |
| 1085 | 451 |
| 452 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
|
453 '''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
|
454 self.voidcmd('QUIT') |
|
1297
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
455 self.close() |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
456 |
|
9234c961ec43
* toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
457 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
|
458 '''Close the connection without assuming anything about it.''' |
| 1085 | 459 self.file.close() |
| 460 self.sock.close() | |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
461 del self.file, self.sock |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
462 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
463 |
|
4927
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
464 import regex |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
465 _150_re = regex.compile("150 .* (\([0-9][0-9]*\) bytes)", regex.casefold) |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
466 |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
467 def parse150(resp): |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
468 '''Parse the '150' response for a RETR request. |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
469 Returns the expected transfer size or None; size is not guaranteed to |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
470 be present in the 150 message. |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
471 ''' |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
472 if resp[:3] != '150': |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
473 raise error_reply, resp |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
474 length = _150_re.match(resp) |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
475 if length >= 0: |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
476 return string.atoi(_150_re.group(1)) |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
477 return None |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
478 |
|
040493de848d
(ftplib.py): Added parse150() function and FTP.ntransfercmd(). This allows
Fred Drake <fdrake@acm.org>
parents:
4345
diff
changeset
|
479 |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
480 def parse227(resp): |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
481 '''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
|
482 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
|
483 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
|
484 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
485 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
|
486 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
|
487 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
|
488 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
|
489 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
|
490 if right < 0: |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
491 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
|
492 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
|
493 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
|
494 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
|
495 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
|
496 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
|
497 return host, port |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
498 # end parse227 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
499 |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
500 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
501 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
|
502 '''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
|
503 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
|
504 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
|
505 |
|
1087
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
506 if resp[:3] <> '257': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
507 raise error_reply, resp |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
508 if resp[3:5] <> ' "': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
509 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
|
510 dirname = '' |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
511 i = 5 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
512 n = len(resp) |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
513 while i < n: |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
514 c = resp[i] |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
515 i = i+1 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
516 if c == '"': |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
517 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
|
518 break |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
519 i = i+1 |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
520 dirname = dirname + c |
|
b9e4f7321e28
Added much functionality, changed some names (errors, login).
Guido van Rossum <guido@python.org>
parents:
1085
diff
changeset
|
521 return dirname |
| 1085 | 522 |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
523 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
|
524 '''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
|
525 print line |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
526 |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
527 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
|
528 '''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
|
529 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
|
530 type = 'TYPE ' + type |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
531 source.voidcmd(type) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
532 target.voidcmd(type) |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
533 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
|
534 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
|
535 # 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
|
536 # transfer request. |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
537 # 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 source.voidresp() |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
543 target.voidresp() |
| 1085 | 544 |
|
5190
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
545 |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
546 class Netrc: |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
547 """Class to parse & provide access to 'netrc' format files. |
|
5190
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
548 |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
549 See the netrc(4) man page for information on the file format. |
|
5190
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
550 |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
551 """ |
|
5190
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
552 __defuser = None |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
553 __defpasswd = None |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
554 __defacct = None |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
555 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
556 def __init__(self, filename=None): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
557 if not filename: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
558 if os.environ.has_key("HOME"): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
559 filename = os.path.join(os.environ["HOME"], ".netrc") |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
560 else: |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
561 raise IOError, "specify file to load or set $HOME" |
|
5190
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
562 self.__hosts = {} |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
563 self.__macros = {} |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
564 fp = open(filename, "r") |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
565 in_macro = 0 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
566 while 1: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
567 line = fp.readline() |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
568 if not line: break |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
569 if in_macro and string.strip(line): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
570 macro_lines.append(line) |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
571 continue |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
572 elif in_macro: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
573 self.__macros[macro_name] = tuple(macro_lines) |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
574 in_macro = 0 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
575 words = string.split(line) |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
576 host = user = passwd = acct = None |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
577 default = 0 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
578 i = 0 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
579 while i < len(words): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
580 w1 = words[i] |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
581 if i+1 < len(words): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
582 w2 = words[i + 1] |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
583 else: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
584 w2 = None |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
585 if w1 == 'default': |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
586 default = 1 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
587 elif w1 == 'machine' and w2: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
588 host = string.lower(w2) |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
589 i = i + 1 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
590 elif w1 == 'login' and w2: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
591 user = w2 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
592 i = i + 1 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
593 elif w1 == 'password' and w2: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
594 passwd = w2 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
595 i = i + 1 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
596 elif w1 == 'account' and w2: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
597 acct = w2 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
598 i = i + 1 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
599 elif w1 == 'macdef' and w2: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
600 macro_name = w2 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
601 macro_lines = [] |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
602 in_macro = 1 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
603 break |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
604 i = i + 1 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
605 if default: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
606 self.__defuser = user or self.__defuser |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
607 self.__defpasswd = passwd or self.__defpasswd |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
608 self.__defacct = acct or self.__defacct |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
609 if host: |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
610 if self.__hosts.has_key(host): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
611 ouser, opasswd, oacct = self.__hosts[host] |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
612 user = user or ouser |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
613 passwd = passwd or opasswd |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
614 acct = acct or oacct |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
615 self.__hosts[host] = user, passwd, acct |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
616 fp.close() |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
617 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
618 def get_hosts(self): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
619 """Return a list of hosts mentioned in the .netrc file.""" |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
620 return self.__hosts.keys() |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
621 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
622 def get_account(self, host): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
623 """Returns login information for the named host. |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
624 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
625 The return value is a triple containing userid, password, and the |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
626 accounting field. |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
627 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
628 """ |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
629 host = string.lower(host) |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
630 user = passwd = acct = None |
|
5190
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
631 if self.__hosts.has_key(host): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
632 user, passwd, acct = self.__hosts[host] |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
633 user = user or self.__defuser |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
634 passwd = passwd or self.__defpasswd |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
635 acct = acct or self.__defacct |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
636 return user, passwd, acct |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
637 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
638 def get_macros(self): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
639 """Return a list of all defined macro names.""" |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
640 return self.__macros.keys() |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
641 |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
642 def get_macro(self, macro): |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
643 """Return a sequence of lines which define a named macro.""" |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
644 return self.__macros[macro] |
|
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
645 |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
646 |
|
5190
a79ecd5af7e5
Added Fred Drake's netrc parser class.
Guido van Rossum <guido@python.org>
parents:
4927
diff
changeset
|
647 |
| 1085 | 648 def test(): |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
649 '''Test program. |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
650 Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...''' |
|
3649
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
651 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
652 debugging = 0 |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
653 rcfile = None |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
654 while sys.argv[1] == '-d': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
655 debugging = debugging+1 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
656 del sys.argv[1] |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
657 if sys.argv[1][:2] == '-r': |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
658 # get name of alternate ~/.netrc file: |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
659 rcfile = sys.argv[1][2:] |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
660 del sys.argv[1] |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
661 host = sys.argv[1] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
662 ftp = FTP(host) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
663 ftp.set_debuglevel(debugging) |
|
5753
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
664 userid = passwd = acct = '' |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
665 try: |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
666 netrc = Netrc(rcfile) |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
667 except IOError: |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
668 if rcfile is not None: |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
669 sys.stderr.write("Could not open account file" |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
670 " -- using anonymous login.") |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
671 else: |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
672 try: |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
673 userid, passwd, acct = netrc.get_account(host) |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
674 except KeyError: |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
675 # no account for host |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
676 sys.stderr.write("No account -- using anonymous login.") |
|
64bc782a42ec
Wrapped up the ~/.netrc support. This is basically just the changes Guido &
Fred Drake <fdrake@acm.org>
parents:
5190
diff
changeset
|
677 ftp.login(userid, passwd, acct) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
678 for file in sys.argv[2:]: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
679 if file[:2] == '-l': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
680 ftp.dir(file[2:]) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
681 elif file[:2] == '-d': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
682 cmd = 'CWD' |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
683 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
|
684 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
|
685 elif file == '-p': |
|
63cdfbb4a2bb
Docstringified and PASV support by Siebren (including new ftpcp() function).
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
686 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
|
687 else: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
688 ftp.retrbinary('RETR ' + file, \ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
689 sys.stdout.write, 1024) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
690 ftp.quit() |
|
2902
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
691 |
|
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
692 |
|
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
693 if __name__ == '__main__': |
|
13581b417837
new sendport() interface; add test() program call
Guido van Rossum <guido@python.org>
parents:
2758
diff
changeset
|
694 test() |
