创建SoapRequest而不使用Suds/Python发送它们
是否仍然可以让sud返回SoapRequest(XML格式),而不发送它 其思想是,我的程序的上层可以使用一个额外的布尔参数(模拟)来调用我的API 我已经实现了一个MessagePlugin,但是我无法获取XML,停止请求并将XML发送回调用方 默认情况下,suds使用名为创建SoapRequest而不使用Suds/Python发送它们,python,xml,soap,suds,Python,Xml,Soap,Suds,是否仍然可以让sud返回SoapRequest(XML格式),而不发送它 其思想是,我的程序的上层可以使用一个额外的布尔参数(模拟)来调用我的API 我已经实现了一个MessagePlugin,但是我无法获取XML,停止请求并将XML发送回调用方 默认情况下,suds使用名为HttpAuthenticated的“传输”类。这就是实际发送发生的地方。所以理论上你可以尝试将其子类化: from suds.client import Client from suds.transport import
HttpAuthenticated的“传输”类。这就是实际发送发生的地方。所以理论上你可以尝试将其子类化:
from suds.client import Client
from suds.transport import Reply
from suds.transport.https import HttpAuthenticated
class HttpAuthenticatedWithSimulation(HttpAuthenticated):
def send(self, request):
is_simulation = request.headers.pop('simulation', False)
if is_simulation:
# don't actually send the SOAP request, just return its XML
return Reply(200, request.headers.dict, request.msg)
return HttpAuthenticated(request)
...
sim_transport = HttpAuthenticatedWithSimulation()
client = Client(url, transport=sim_transport,
headers={'simulation': is_simulation})
有点粗糙。(例如,这依赖于HTTP头将布尔模拟选项向下传递到传输级别。)但我希望这说明了这一想法。我实现的解决方案是:
class CustomTransportClass(HttpTransport):
def __init__(self, *args, **kwargs):
HttpTransport.__init__(self, *args, **kwargs)
self.opener = MutualSSLHandler() # I use a special opener to enable a mutual SSL authentication
def send(self,request):
print "===================== 1-* request is going ===================="
is_simulation = request.headers['simulation']
if is_simulation == "true":
# don't actually send the SOAP request, just return its XML
print "This is a simulation :"
print request.message
return Reply(200, request.headers, request.message )
return HttpTransport.send(self,request)
sim_transport = CustomTransportClass()
client = Client(url, transport=sim_transport,
headers={'simulation': is_simulation})
谢谢你的帮助,嗨,谢谢你的回复。我已经在使用另一个HttpTransport类来执行Ssl相互身份验证。从理论上讲,如果我只是用您的示例声明send方法,它应该可以工作吗?我明天会试试的。你的建议对我很有效。我刚刚做了一些小修改。我将用解决方案编辑我的帖子
class CustomTransportClass(HttpTransport):
def __init__(self, *args, **kwargs):
HttpTransport.__init__(self, *args, **kwargs)
self.opener = MutualSSLHandler() # I use a special opener to enable a mutual SSL authentication
def send(self,request):
print "===================== 1-* request is going ===================="
is_simulation = request.headers['simulation']
if is_simulation == "true":
# don't actually send the SOAP request, just return its XML
print "This is a simulation :"
print request.message
return Reply(200, request.headers, request.message )
return HttpTransport.send(self,request)
sim_transport = CustomTransportClass()
client = Client(url, transport=sim_transport,
headers={'simulation': is_simulation})