根据文档和之前所有的SO问题,这里的代码应该可以正常工作,但不是。找不到任何重复。如果有的话,我的错。在
基本上,在对一个有效的ftp连接调用ftp.quit()之后,后续的测试它是否连接的尝试都失败了。在ftp.quit()之前运行下面的try/except块可以正常工作(“Connection is open”总是被打印出来)。在
使用关闭连接后ftp.退出():
1。我试图在调用ftp.voidcmd('NOOP')时捕获错误。在
我得到一个Attribute: NoneType...错误(这应该是ftp对象重置为None的一个指示)。在
2.但是,检查if ftp is None也失败了。在
3:为了进一步确认,我检查type(ftp)
要复制的代码:(结果如下)# Connect
import ftplib
ftp = ftplib.FTP("FtpSite")
ftp.login("user","passwd")
# show success
print(ftp.getwelcome())
connected = True
# Close connection
ftp.quit()
# Is ftp still a valid obj?
print(type(ftp))
# Is ftp None?
if ftp is None:
print("ftp is None.")
else:
print("ftp is still assigned, but closed")
# check if open
try:
ftp.voidcmd("NOOP")
except ftplib.all_errors as e:
errorInfo = str(e).split(None, 1)[0]
print(errorInfo)
connected = False
if connected:
print("Connection is open.")
else:
print("Connection already closed.")
结果:
^{pr2}$