python web-services:使用 ZSI 从服务器返回错误
我有兴趣为 Web 服务编写一个 Python 客户端,出于测试目的,拥有一个简单的存根服务器也会非常有趣。 我正在使用 python 2.3 和 ZSI 2.0。
我的问题是我无法从服务器返回异常。
如果我在 wsdl 中引发用于肥皂故障的类型的异常,我会收到 TypeError '异常必须是类、实例或字符串(已弃用),而不是 EmptyStringException_Def'。 我认为这意味着错误对象不是 Exception 的子类,但是以这种方式修改生成的代码没有帮助 - 当然,不必修改生成的代码会好得多:)
如果我返回错误对象作为响应的一部分,它只是被忽略。
我找不到任何有关 ZSI 中的故障处理的文档。 有什么提示吗?
下面是一个非常简单服务的服务器的示例代码,只有一个方法,spellBackwards,如果输入字符串为空,它应该返回一个soap错误:
#!/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(),])
I'm interested in writing a python client for a web-service, and for testing purposes it would be very interesting also to have a simple stub server. I'm using python 2.3, and ZSI 2.0.
My problem is that I do not manage to return an exception from the server.
If I raise an exception of the type used for the soap fault in the wsdl, I get the TypeError 'exceptions must be classes, instances, or strings (deprecated), not EmptyStringException_Def'. I thought this meant that the fault object was not a subclass of Exception, but modifying the generated code in this way did not help - and of course, not having to modify the generated code would be much better :)
If I return the fault object as part of the response, it is just ignored.
I couldn't find any documentation about faults handling in ZSI. Any hints?
Here's a sample code for a server of a very simple service with just one method, spellBackwards, which should return a soap fault if the input string is empty:
#!/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(),])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Chris Hoobs 的这本 ZSI Cookbook 中找到了答案,链接在ZSI 主页:
我认为这是正确的,因为该论文是从项目主页链接的。
本文还提出了一种解决方法,其中包括修补 ZSI 发行版中的 Fault.py 文件。
我测试了解决方法,它按承诺工作; 修补库对我来说是可以接受的解决方案,因为我需要生成一个仅用于测试目的的服务器(即我不需要分发修补后的库)。
I've found the answer in this ZSI Cookbook, by Chris Hoobs, linked at the bottom of the ZSI home page:
I assume this to be correct since the paper is linked from the project home page.
This paper also suggests a workaround, which consists in patching the Fault.py file in the ZSI distribution.
I tested the workaround and it works as promised; patching the library is as acceptable solution for me since I need to generate a server for test purposes only (i.e. I'll not need to distribute the patched library).
很抱歉无法回答这个问题。
我和ZSI战斗了一段时间。
我现在使用 SUDS : https://fedorahosted.org/suds/wiki ,一切都有变得更加简单。
apologies for not being able to answer the question.
I battled with ZSI for a while.
I'm now using SUDS : https://fedorahosted.org/suds/wiki , and everything has become much simpler.