【问题标题】:python web-services: returning a fault from the server using ZSIpython web-services:使用 ZSI 从服务器返回故障
【发布时间】:2009-01-15 14:24:18
【问题描述】:

我有兴趣为 web 服务编写一个 python 客户端,并且为了测试目的,拥有一个简单的存根服务器也会非常有趣。我正在使用 python 2.3 和 ZSI 2.0。

我的问题是我无法从服务器返回异常。

如果我在 wsdl 中引发用于 soap 错误的类型的异常,我会得到 TypeError '异常必须是类、实例或字符串(已弃用),而不是 EmptyStringException_Def'。我认为这意味着错误对象不是 Exception 的子类,但是以这种方式修改生成的代码并没有帮助 - 当然,不必修改生成的代码会好得多:)

如果我将故障对象作为响应的一部分返回,它就会被忽略。

我找不到任何关于 ZSI 中的故障处理的文档。有什么提示吗?

这是一个非常简单的服务服务器的示例代码,只有一个方法,spellBackwards,如果输入字符串为空,它应该返回一个肥皂错误:

#!/usr/bin/env python
from ZSI.ServiceContainer import AsServer
from SpellBackwardsService_services_server import *
from SpellBackwardsService_services_types import *
class SpellBackwardsServiceImpl(SpellBackwardsService):
    def soap_spellBackwards(self, ps):
        response = SpellBackwardsService.soap_spellBackwards(self, ps)
        input = self.request._in
        if len(input) != 0:
            response._out = input[::-1]
        else:
            e = ns0.EmptyStringException_Def("fault")
            e._reason = "Empty input string"

            # The following just produces an empty return message:
            # response._fault = e

            # The following causes TypeError
            # raise e

        return response

AsServer(port=8666, services=[SpellBackwardsServiceImpl(),])

【问题讨论】:

    标签: python web-services fault zsi


    【解决方案1】:

    我在ZSI Cookbook 中找到了答案,作者是 Chris Hoobs,链接在ZSI home page 的底部:

    5.4 例外
    一个棘手的问题是如何在服务器上产生故障。使用 ZSI v2.0 代码为 提供了,这是不可能的。

    我认为这是正确的,因为该论文是从项目主页链接的。
    本文还提出了一种解决方法,其中包括修补 ZSI 发行版中的 Fault.py 文件。
    我测试了解决方法,它按承诺工作;修补库对我来说是可接受的解决方案,因为我需要生成一个服务器仅用于测试目的(即我不需要分发修补的库)。

    【讨论】:

      【解决方案2】:

      抱歉无法回答问题。

      我和ZSI战斗了一段时间。

      我现在使用的是 SUDS : https://fedorahosted.org/suds/wiki ,一切都变得简单多了。

      【讨论】:

      • 好吧,在我看来,将我指向另一个库是一个非常有效的答案:) 谢谢。无论如何,描述说“Suds 是一个用于消费 Web 服务的轻量级 SOAP python 客户端”,它是否也允许我编写一个简单的服务器?
      • 不,恐怕只有客户端。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      相关资源
      最近更新 更多