Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 抑制twisted中的异常_Python_Twisted - Fatal编程技术网

Python 抑制twisted中的异常

Python 抑制twisted中的异常,python,twisted,Python,Twisted,我正在编写一个定制的ftp服务器,我需要禁止将异常(一种特定类型的异常)打印到控制台,但我希望服务器将“550请求的操作未采取:内部服务器错误”或类似的内容发送到客户端 然而,当我使用AdderBack()捕获异常时,我在控制台中看不到异常,但客户端获得OK状态 我该怎么办?当您在errback处理程序中发现错误时,应检查故障类型,并根据应用程序的内部逻辑将错误作为FTP错误消息发送给客户端 twisted.protocol.ftp.ftp使用self.reply(错误代码,“说明”)处理此问题

我正在编写一个定制的ftp服务器,我需要禁止将异常(一种特定类型的异常)打印到控制台,但我希望服务器将“550请求的操作未采取:内部服务器错误”或类似的内容发送到客户端

然而,当我使用AdderBack()捕获异常时,我在控制台中看不到异常,但客户端获得OK状态


我该怎么办?

当您在errback处理程序中发现错误时,应检查故障类型,并根据应用程序的内部逻辑将错误作为FTP错误消息发送给客户端 twisted.protocol.ftp.ftp使用self.reply(错误代码,“说明”)处理此问题

因此,您的代码可以如下所示:

from twisted.internet import ftp

MY_ERROR = ftp.REQ_ACTN_NOT_TAKEN

def failureCheck(failureInstance):
    #do some magic to establish if we should reply an Error to this failure
    return True

class myFTP(ftp.FTP):
    def myActionX(self):
        magicResult = self.doDeferredMagic()
        magicResult.addCallback(self.onMagicSuccess)
        magicResult.addErrback(self.onFailedMagic)
    def onFailedMagic(self,failureInstance):
        if failureCheck(failureInstance):
            self.reply(MY_ERROR,'Add relevant failure information here')
        else:
            #do whatever other logic here
            pass