【问题标题】:Checking FTP connection is valid using NOOP command使用 NOOP 命令检查 FTP 连接是否有效
【发布时间】: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 这样简单的东西

标签: python ftp ftplib noop


【解决方案1】:

连接到服务器:

ftp = ftplib.FTP('CENSORED')

所以,NOOP 命令自然会成功,因为它不需要经过身份验证的连接。

您的connect_ftp 是正确的,只是您需要在connect 调用中指定主机名。

【讨论】:

  • 没有登录?
  • 连接和登录在 FTP 中是两个不同的东西。 NOOP 命令不需要经过身份验证的连接。
  • 这让我大吃一惊,我几乎要疯了,它是如此简单。抱歉浪费了你的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
相关资源
最近更新 更多