Python 捕获由ftplib中的连接错误导致的异常

Python 捕获由ftplib中的连接错误导致的异常,python,exception,ftplib,Python,Exception,Ftplib,我正在尝试使用python的ftplib 我应该捕获什么样的异常以确保问题是连接错误(以便我可以重新连接) 编辑: 在这种情况下,我尝试了所有错误: 通过ftplib连接到FTP服务器,并在文件上载之前暂停应用程序(通过调试器) 通过服务器的闭合连接 恢复申请 使用此代码: try: self.f.cwd(dest) return self.f.storbinary(('STOR '+n).encode('ut

我正在尝试使用python的
ftplib

我应该捕获什么样的异常以确保问题是连接错误(以便我可以重新连接)


编辑:
在这种情况下,我尝试了所有错误:

  • 通过
    ftplib
    连接到FTP服务器,并在文件上载之前暂停应用程序(通过调试器)
  • 通过服务器的闭合连接
  • 恢复申请
使用此代码:

        try:        
            self.f.cwd(dest)
            return self.f.storbinary(('STOR '+n).encode('utf-8'), open(f,'rb'))
        except ftplib.all_errors as e:
            print e
捕获异常,但所有错误均为空:

e   EOFError:   
    args    tuple: ()   
    message str:    

您可以在文档中查找:

也别忘了:

像这样试试

import socket
import ftplib

try:
    s = ftplib.FTP(server , user , password) 
except ftplib.all_errors as e:
    print "%s" % e

捕获ftp服务器之间的异常的简单方法可能是:

import ftplib, os

def from_ftp( server, path, data, filename = None ):
    '''Store the ftp data content to filename (anonymous only)'''
    if not filename:
        filename = os.path.basename( os.path.realpath(data) )

    try:
        ftp = ftplib.FTP( server )
        print( server + ' -> '+ ftp.login() )        
        print( server + ' -> '+ ftp.cwd(path) ) 
        with open(filename, 'wb') as out:
            print( server + ' -> '+ ftp.retrbinary( 'RETR ' + data, out.write ) ) 

    except ftplib.all_errors as e:
        print( 'Ftp fail -> ', e )
        return False

    return True

def to_ftp( server, path, file_input, file_output = None ):
    '''Store a file to ftp (anonymous only)'''
    if not file_output:
        file_output = os.path.basename( os.path.realpath(file_input) )

    try:
        ftp = ftplib.FTP( server )
        print( server + ' -> '+ ftp.login() )        
        print( server + ' -> '+ ftp.cwd(path) ) 
        with open( file_input, 'rb' ) as out:
            print( server + ' -> '+ ftp.storbinary( 'STOR ' + file_output, out ) ) 

    except ftplib.all_errors as e:
        print( 'Ftp fail -> ', e )
        return False

    return True

我不明白你想告诉我们什么。所以
ftplib。所有的\u错误都有效,但它是空的?@dav1d:是的;(检查我的第二次编辑);不总是,但大多数时候它有一个空元组!(当它被捕获时!!)
e
是被捕获错误的实例,它仅仅意味着初始化时没有向EOR传递任何参数。那没什么你要关心的。