summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-01-21 20:48:12 (GMT)
committerGitHub <noreply@github.com>2024-01-21 20:48:12 (GMT)
commit4c42d981542372f57bc0071481dee42dbf119927 (patch)
tree43720bf72f8814f2af6b5719cb8c87c87e1dd3f7 /Lib
parent5183a5dee525916d7dca9ab0b443067be257f86f (diff)
downloadcpython-4c42d981542372f57bc0071481dee42dbf119927.zip
cpython-4c42d981542372f57bc0071481dee42dbf119927.tar.gz
cpython-4c42d981542372f57bc0071481dee42dbf119927.tar.bz2
[3.12] gh-114241: Fix and improve the ftplib CLI (GH-114242) (GH-114404)
* Fix writing the retrieved binary file to stdout. * Add a newline after writing warnings to stderr. * Fix a TypeError if the netrc file doesn't contain a host/default entry. * Improve the usage message. (cherry picked from commit 42d72b23dd1ee0e100ee47aca64fc1e1bbe576c9) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ftplib.py27
1 files changed, 17 insertions, 10 deletions
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
index a56e0c3..10c5d1e 100644
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -900,11 +900,17 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
def test():
'''Test program.
- Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
+ Usage: ftplib [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
- -d dir
- -l list
- -p password
+ Options:
+ -d increase debugging level
+ -r[file] set alternate ~/.netrc file
+
+ Commands:
+ -l[dir] list directory
+ -d[dir] change the current directory
+ -p toggle passive and active mode
+ file retrieve the file and write it to stdout
'''
if len(sys.argv) < 2:
@@ -930,15 +936,14 @@ def test():
netrcobj = netrc.netrc(rcfile)
except OSError:
if rcfile is not None:
- sys.stderr.write("Could not open account file"
- " -- using anonymous login.")
+ print("Could not open account file -- using anonymous login.",
+ file=sys.stderr)
else:
try:
userid, acct, passwd = netrcobj.authenticators(host)
- except KeyError:
+ except (KeyError, TypeError):
# no account for host
- sys.stderr.write(
- "No account -- using anonymous login.")
+ print("No account -- using anonymous login.", file=sys.stderr)
ftp.login(userid, passwd, acct)
for file in sys.argv[2:]:
if file[:2] == '-l':
@@ -951,7 +956,9 @@ def test():
ftp.set_pasv(not ftp.passiveserver)
else:
ftp.retrbinary('RETR ' + file, \
- sys.stdout.write, 1024)
+ sys.stdout.buffer.write, 1024)
+ sys.stdout.buffer.flush()
+ sys.stdout.flush()
ftp.quit()