FTP服务器在下载完成前中断连接

2024-10-01 09:23:52 发布

您现在位置:Python中文网/ 问答频道 /正文

有时,FTP服务器会在文件完全下载之前关闭连接。。你知道吗

这是我的密码:

ftp = ftplib.FTP(site)
ftp.login(user, pw)
ftp.cwd(dir)
remotefiles = ftp.nlst()
for file in remotefiles:
    if fnmatch.fnmatch(file, match_text):
        if os.path.exists(file):
            if True: print file, 'already fetched'
        else:
            if True: print 'Downloading', file
            local = open(file, 'wb')                
            try:            
                ftp.retrbinary('RETR ' + file, local.write)
            finally:
                local.close()                       
            if True: print 'Download done.' 

Tags: 文件服务器true密码iflocalsiteftp
1条回答
网友
1楼 · 发布于 2024-10-01 09:23:52

您可以在FTP构造函数中指定一个timeout参数,并将其设置为0或一些非常大的值,如系统最大值。你知道吗

class ftplib.FTP([host[, user[, passwd[, acct[, timeout]]]]])

enter image description here

此外,您可以打开调试来查看幕后的情况。你知道吗

ftp = ftplib.FTP(site, user, pw, timeout=0)
ftp.set_debuglevel(2)

希望这有帮助。你知道吗

相关问题 更多 >