Benefits of twisted-suds - Async way of using python suds soap lib

前端 未结 1 1531
孤独总比滥情好
孤独总比滥情好 2021-02-10 16:54

I\'m using python suds library to make a SOAP client based on a local wsdl file. My goal is to use Twisted as the backend so I query the SOAP servers in a asyncronous way.

1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-10 17:47

    I'm using suds in my projects. I didn't have to do any patches, or use twisted-suds. I'm using the 0.4.1-2 version of python-suds package (on ubuntu) and it comes with very usefull nosend option.

        # This parses the wsdl file. The autoblend option you'd probably skip, 
        # its needed when name spaces are not strictly preserved (case for Echo Sign).
        from suds import client
        self._suds = client.Client('file://' + config.wsdl_path, nosend=True,
                                   autoblend=True)
    
        ....
    
        # Create a context for the call, example sendDocument() call. This doesn't yet
        # send anything, only creates an object with the request and capable of parsing
        # the response
        context = self._suds.service.sendDocument(apiKey=....)
    
        # Actually send the request. Use any web client you want. I actually use 
        # something more sophisticated, but below I put the example using 
        # standard twisted web client.
        from twisted.web import client
        d = client.getPage(url=context.client.location(), 
                           postdata=str(context.envelope),
                           method='POST',
                           headers=context.client.headers())
    
        # The callback() of the above Deferred is fired with the body of the 
        # http response. I parse it using the context object.          
        d.addCallback(context.succeeded)
    
        # Now in the callback you have the actual python object defined in 
        # your WSDL file. You can print...
        from pprint import pprint
        d.addCallback(pprint)
        # I the response is a failure, your Deferred would be errbacked with 
        # the suds.WebFault exception.
    

    0 讨论(0)
提交回复
热议问题