【发布时间】:2019-04-04 13:51:48
【问题描述】:
我的一个脚本在批量作业期间似乎与我的 FTP 断开连接时遇到问题。为了解决这个问题,我尝试制作如下所示的模块:
def connect_ftp(ftp):
print "ftp1"
starttime = time.time()
retry = False
try:
ftp.voidcmd("NOOP")
print "ftp2"
except:
retry = True
print "ftp3"
print "ftp4"
while (retry):
try:
print "ftp5"
ftp.connect()
ftp.login('LOGIN', 'CENSORED')
print "ftp6"
retry = False
print "ftp7"
except IOError as e:
print "ftp8"
retry = True
sys.stdout.write("\rTime disconnected - "+str(time.time()-starttime))
sys.stdout.flush()
print "ftp9"
我只使用调用函数:
ftp = ftplib.FTP('CENSORED')
connect_ftp(ftp)
但是,我已经使用print 行跟踪代码如何运行,并且在第一次使用模块时(甚至在 FTP 连接之前)我的脚本运行 ftp.voidcmd("NOOP") 并且没有除了它,因此最初不会尝试连接到 FTP。
输出是:
ftp1
ftp2
ftp4
ftp success #this is ran after the module is called
我承认我的代码不是最好或最漂亮的,我还没有实现任何东西来确保如果我一直无法重新连接,我不会经常重新连接,但我不知道为什么会这样t 为我的生活工作,所以我还没有看到扩展模块的意义。这甚至是连接/重新连接到 FTP 的最佳方法吗?
提前谢谢你
【问题讨论】:
-
我已根据要求修改了问题。我还按照建议交换了 .connect() 和 login() 。 @MartinPrikryl
-
只有像 connect_ftp(ftp) @MartinPrikryl 这样简单的东西