【问题标题】:Create SoapRequest without sending them with Suds/Python创建 SoapRequest 而不使用 Suds/Python 发送它们
【发布时间】:2014-09-24 17:37:15
【问题描述】:

有没有办法让 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.

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

问候

【问题讨论】:

    标签: python xml soap suds


    【解决方案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})
    

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

    【讨论】:

    • 您好,感谢您的回复。我已经在使用另一个 HttpTransport 类来执行 Ssl 相互身份验证stackoverflow.com/questions/6277027/suds-over-https-with-cert。理论上,如果我只是用您的示例声明发送方法,它应该可以工作吗?我明天试试看
    • 您的建议对我来说非常有效。我刚刚做了一些小的修改。我将使用解决方案编辑我的帖子
    【解决方案2】:

    我实现的解决方案是:

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

    感谢您的帮助,

    【讨论】:

      猜你喜欢
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多