Skip to content

Commit 58c93d8

Browse files
[3.11] gh-114241: Fix and improve the ftplib CLI (GH-114242) (GH-114405)
* 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 42d72b2) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 9f164ab commit 58c93d8

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
@@ -922,11 +922,17 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
922922

923923
def test():
924924
'''Test program.
925-
Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
925+
Usage: ftplib [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
926926
927-
-d dir
928-
-l list
929-
-p password
927+
Options:
928+
-d increase debugging level
929+
-r[file] set alternate ~/.netrc file
930+
931+
Commands:
932+
-l[dir] list directory
933+
-d[dir] change the current directory
934+
-p toggle passive and active mode
935+
file retrieve the file and write it to stdout
930936
'''
931937

932938
if len(sys.argv) < 2:
@@ -952,15 +958,14 @@ def test():
952958
netrcobj = netrc.netrc(rcfile)
953959
except OSError:
954960
if rcfile is not None:
955-
sys.stderr.write("Could not open account file"
956-
" -- using anonymous login.")
961+
print("Could not open account file -- using anonymous login.",
962+
file=sys.stderr)
957963
else:
958964
try:
959965
userid, acct, passwd = netrcobj.authenticators(host)
960-
except KeyError:
966+
except (KeyError, TypeError):
961967
# no account for host
962-
sys.stderr.write(
963-
"No account -- using anonymous login.")
968+
print("No account -- using anonymous login.", file=sys.stderr)
964969
ftp.login(userid, passwd, acct)
965970
for file in sys.argv[2:]:
966971
if file[:2] == '-l':
@@ -973,7 +978,9 @@ def test():
973978
ftp.set_pasv(not ftp.passiveserver)
974979
else:
975980
ftp.retrbinary('RETR ' + file, \
976-
sys.stdout.write, 1024)
981+
sys.stdout.buffer.write, 1024)
982+
sys.stdout.buffer.flush()
983+
sys.stdout.flush()
977984
ftp.quit()
978985

979986

0 commit comments

Comments
 (0)