【发布时间】:2021-02-24 10:06:02
【问题描述】:
我是 Python 中 ftplib 和 FTP 的新手,我正在测试文档提供的一个简单脚本:https://docs.python.org/3/library/ftplib.html#ftplib.error_reply
try:
ftp = ftplib.FTP('ftp.us.debian.org')
ftp.login('111', '111')
except ftplib.all_errors as err:
print(f'Error: ' + str(err))
这返回Error: 503 Login with USER first.,如果我添加ftp.set_debuglevel(2),这将告诉我更详细的信息:
*cmd* 'USER 111'
*put* 'USER 111\r\n'
*get* '331 This FTP server is anonymous only.\n'
*resp* '331 This FTP server is anonymous only.'
*cmd* 'PASS ***'
*put* 'PASS ***\r\n'
*get* '503 Login with USER first.\n'
*resp* '503 Login with USER first.'
Error: 503 Login with USER first.
它告诉我其他信息,例如This FTP server is anonymous only。
所以我也试过了:
try:
ftp = ftplib.FTP('ftp.us.debian.org')
except ftplib.all_errors as err:
print(f'Error: ' + str(err))
try:
#ftp.set_debuglevel(2)
ftp.login('111', '111')
except ftplib.all_errors as err:
print(f'Error: ' + str(err))
这仍然不会告诉我This FTP server is anonymous only
我想知道我是否遗漏了什么,有人可以告诉我为 FTP 进行异常处理的最佳做法是什么?有什么例子吗?非常感谢。
【问题讨论】:
标签: python exception ftp ftplib