python - Spyne 故障 - HTTP 返回代码

标签 python http exception spyne

我已阅读有关 Spyne 故障 ( http://spyne.io/docs/2.10/manual/03_types.html ) 的信息,但无法使用 400 返回代码正确引发故障。我的错误响应已正确形成,但当我需要返回 400 时,HTTP 返回代码为 500。

@srpc(Boolean, _returns=String)
def requestConfiguration(value):
    #if value is true, get all the data
    if value == True:
        #todo - get the config
        return 'True Received'

    else:
        # if anything other than True is received, MUST respond with a SOAP fault and HTTP 400
        raise Fault(faultcode="Client.", faultstring="Value must be True")
        # raise error.InvalidInputError("problem", "problem")

阅读一些文档(http://spyne.io/docs/2.10/modules/spyne/model/fault.html#Fault),我将其解释为FaultCode必须是以Client开头的字符串,它将返回400错误。 (我知道 if-else 很糟糕,我只是想在正确编写代码之前获得概念验证)

我认为我需要对错误进行子类化,而不是仅仅提出它,但我无法理解它。我深入研究代码/protocol/soap/soap11 并发现故障_to_http_reponse_code 只是返回 HTTP 500。

提前致谢

最佳答案

我放弃了子类方法,而是更新了soap11.py中的fault_to_http_reponse_code函数。这是一个很糟糕的补丁,但它完成了我想要的工作。

def fault_to_http_response_code(self, fault):
    from spyne.const.http import HTTP_400, HTTP_401, HTTP_404, HTTP_405, HTTP_413, HTTP_500
    from spyne.error import Fault, InternalError, ResourceNotFoundError, RequestTooLongError, RequestNotAllowed, InvalidCredentialsError
    if isinstance(fault, RequestTooLongError):
        return HTTP_413
    if isinstance(fault, ResourceNotFoundError):
        return HTTP_404
    if isinstance(fault, RequestNotAllowed):
        return HTTP_405
    if isinstance(fault, InvalidCredentialsError):
        return HTTP_401
    if isinstance(fault, Fault) and (fault.faultcode.startswith('Client.')
                                            or fault.faultcode == 'Client'):
        return HTTP_400

    return HTTP_500

然后我只是提出一个正常的错误,错误代码以 Client 开头。

raise Fault(faultcode="Client.", faultstring="value must be True)

希望有人能提供适当的方法来解决这个问题。

关于python - Spyne 故障 - HTTP 返回代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44674096/

相关文章:

C++ 赋值运算符异常安全

visual-studio-2010 - 解决第一次异常

python Pandas : How can I group by and assign an id to all the items in a group?

Python初始化随机值的多维numpy数组

python - 在 python 中用 re.sub 替换特定的命名组

http - 填写表单值的 POST 请求以登录

java - 如何让我的 Android 应用程序更快地接收/发送数据?

java - Jersey 使用 POST 接受文件参数和字符串

java - ReSTLet 在 HTTPS BindException 上与 Jetty 一起运行

python - 用于语音识别的流输入