创建SoapRequest而不使用Suds / Python发送它们

时间:2014-09-24 17:37:16

标签: python xml soap suds

有没有让suds返回SoapRequest(用XML格式)而不发送它?

我的想法是我的程序的上层可以使用额外的布尔参数(模拟)调用我的API。

If simulation == false then process the other params and send the request via suds
If simulation == false then process the other params, create the XML using suds (or any other way) and return it to the caller without sending it to the host.

我已经实现了一个MessagePlugin https://fedorahosted.org/suds/wiki/Documentation#MessagePlugin,但我无法获取XML,停止请求并将XML发回给调用者......

此致

2 个答案:

答案 0 :(得分:1)

suds使用"运输"默认情况下调用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})

有点hacky。 (例如,这依赖于HTTP标头将布尔模拟选项传递到传输级别。)但我希望这说明了这个想法。

答案 1 :(得分:0)

我实施的解决方案是:

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})

感谢您的帮助,