Skip to content

Commit 42d72b2

Browse files
gh-114241: Fix and improve the ftplib CLI (GH-114242)
* 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.
1 parent 3360301 commit 42d72b2

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Lib/ftplib.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,17 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
900900

901901
def test():
902902
'''Test program.
903-
Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
903+
Usage: ftplib [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
904904
905-
-d dir
906-
-l list
907-
-p password
905+
Options:
906+
-d increase debugging level
907+
-r[file] set alternate ~/.netrc file
908+
909+
Commands:
910+
-l[dir] list directory
911+
-d[dir] change the current directory
912+
-p toggle passive and active mode
913+
file retrieve the file and write it to stdout
908914
'''
909915

910916
if len(sys.argv) < 2:
@@ -930,15 +936,14 @@ def test():
930936
netrcobj = netrc.netrc(rcfile)
931937
except OSError:
932938
if rcfile is not None:
933-
sys.stderr.write("Could not open account file"
934-
" -- using anonymous login.")
939+
print("Could not open account file -- using anonymous login.",
940+
file=sys.stderr)
935941
else:
936942
try:
937943
userid, acct, passwd = netrcobj.authenticators(host)
938-
except KeyError:
944+
except (KeyError, TypeError):
939945
# no account for host
940-
sys.stderr.write(
941-
"No account -- using anonymous login.")
946+
print("No account -- using anonymous login.", file=sys.stderr)
942947
ftp.login(userid, passwd, acct)
943948
for file in sys.argv[2:]:
944949
if file[:2] == '-l':
@@ -951,7 +956,9 @@ def test():
951956
ftp.set_pasv(not ftp.passiveserver)
952957
else:
953958
ftp.retrbinary('RETR ' + file, \
954-
sys.stdout.write, 1024)
959+
sys.stdout.buffer.write, 1024)
960+
sys.stdout.buffer.flush()
961+
sys.stdout.flush()
955962
ftp.quit()
956963

957964

0 commit comments

Comments
 (0)