微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python FTPlib随机错误输出-[Errno 111]连接被拒绝

如何解决Python FTPlib随机错误输出-[Errno 111]连接被拒绝

使用Python FTPlib从FTP服务器下载文件。尽管连接是随机成功的,但它仍然会遇到Connection拒绝异常,有时会下载部分数据。 不确定为什么会发生这种情况以及如何解决此问题。 下面是我的代码

class fileTransfer(object):

    def main(self):

        # Arguments
        host        = 'ftp.myHost.com'
        src_dir     = 'Test_Location'
        src_frmt    = 'test_files'
        uname       = 'MyUserName'
        pwd         = 'xxxxxx'
        
        print('*********************** File Transfer **************************')
        print('   Host name       : ' + host)
        print('   FTP Location    : ' + src_dir)
        print('   File Pattern    : ' + src_frmt)
       
        # Call the function to Transfer Files
        filesDownload(host,uname,pwd,src_dir,src_frmt)
        

def filesDownload(host,src_frmt):
    # Connection to FTP 
    try:
        print("INFO: Connecting to FTP...")
        ftps = FTP_TLS('{0}'.format(host))
        ftps.login(user = '{0}'.format(uname),passwd = '{0}'.format(pwd))
        print("INFO: Connection to FTP Successful")
    except Exception as e:
        print(e)
    
    # Change FTP Location
    print("INFO: Changing Directory")
    ftps.cwd(src_dir)
    # List of files to download based on source pattern 
    files = ftps.nlst(src_frmt + '*')
    print("INFO: Number of files to download - " + str(len(files)) + "\nINFO: Files to download are - \n" + '\n'.join(files))
    
    # Download files
    print("INFO: Initiating files download")
    try:
        for f in files:
            with open(f,'wb') as fh:
                ftps.retrbinary('RETR ' + f,fh.write)
        print("\nINFO: Files Downloaded to local successfully.")
    except Exception as e:
        print(e)


#  Main Function
if __name__ == "__main__":
    print(" ~~ Running fileTransfer ~~ ")
try:
    fileTransfer().main()
except Exception as e:
    print(' ##### Error running fileTransfer ##### ')
    raise Exception(e)

我得到的错误

 ~~ Running fileTransfer ~~ 
*********************** File Transfer **************************
   Host name       : ftp.myHost.com
   FTP Location    : Test_Location
   File Pattern    : test_files
INFO: Connecting to FTP...
INFO: Connection to FTP Successful
INFO: Changing Directory
INFO: Number of files to download - 3
INFO: Files to download are - 
test_files_1.xlsx
test_files_2.xlsx
test_files_3.xlsx
INFO: Initiating files download
[Errno 111] Connection refused

似乎连接成功,但是在下载文件时发生了拒绝连接。

有人能建议出什么问题以及如何解决这个问题。

谢谢..

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。