[英]python web-services: returning a fault from the server using ZSI
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. 我有兴趣为网络服务编写python客户端,并且出于测试目的,拥有一个简单的存根服务器也将非常有趣。 I'm using python 2.3, and ZSI 2.0. 我正在使用python 2.3和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'. 如果在wsdl中引发用于肥皂故障的类型的异常,则会得到TypeError'异常必须是类,实例或字符串(不建议使用),而不是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 :) 我认为这意味着故障对象不是Exception的子类,但是以这种方式修改生成的代码无济于事-当然,不必修改生成的代码会更好:)
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. 我在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: 这是使用非常简单的服务的服务器的示例代码,只有一种方法,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(),])
I've found the answer in this ZSI Cookbook , by Chris Hoobs, linked at the bottom of the ZSI home page : 我在Chris Hoobs撰写的ZSI Cookbook中找到了答案,链接在ZSI主页底部:
5.4 Exceptions 5.4例外情况
A thorny question is how to generate the faults at the server. 一个棘手的问题是如何在服务器上生成故障。 With the ZSI v2.0 code as it is provided, this is not possible. 使用所提供的ZSI v2.0代码,这是不可能的。
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. 本文还提出了一种解决方法,该方法包括修补ZSI发行版中的Fault.py文件。
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 (ie I'll not need to distribute the patched library). 修补库对我来说是可以接受的解决方案,因为我只需要生成用于测试目的的服务器即可(即,我不需要分发修补后的库)。
apologies for not being able to answer the question. 对于无法回答该问题表示歉意。
I battled with ZSI for a while. 我和ZSI战斗了一段时间。
I'm now using SUDS : https://fedorahosted.org/suds/wiki , and everything has become much simpler. 我现在正在使用SUDS: https : //fedorahosted.org/suds/wiki ,一切都变得更加简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.