【发布时间】:2019-11-17 01:21:32
【问题描述】:
我有一个程序在用户发出命令时与 FTP 服务器交互。这是我的代码的基本结构:
from ftplib import FTP
ftp = FTP(host=host)
login_status = ftp.login(user=username, passwd=password)
while True:
command = input()
if command == "abc":
ftp.storbinary(textfile, textmessage1)
elif command == "def":
ftp.storbinary(textfile, textmessage2)
问题是,如果我在发出命令之间等待大约 20 秒(即,如果我离开程序大约 20 秒),并在 20 秒间隙后尝试发出命令,则会弹出此错误消息:ftplib.error_temp: 421 Timeout - try typing a little faster next time
据我了解,ftp 服务器有时间限制,在不活动后会踢你。我正在寻找一种让 FTP 服务器保持忙碌并停止让它启动我的程序的方法。基本上,任何阻止该错误消息再次显示的解决方案。
提前致谢!
【问题讨论】:
-
如果您尝试像这样设置超时:ftp = FTP(host=host, timeout=100)
-
@GiovaniSalazar 所以如果我为超时参数设置一个非常大的数字,FTP 服务器会有更大的超时余量吗?另外,timeout 变量的单位是秒还是毫秒?
-
当然不是,
timeout参数仅供本地使用。如果对服务器没有影响。