Python SOAP Client - use SUDS or something else?

后端 未结 4 1617
独厮守ぢ
独厮守ぢ 2021-01-29 22:33

I am currently looking into implementing a client which will use an existing extensive SOAP management API.

I looked into different SOAP implementations like pysimples

相关标签:
4条回答
  • 2021-01-29 22:54

    It's 2013. This is an update for anyone who encountered the problem with Python and SOAP like me.

    I was trying to use SOAP in Python. I tried out suds, but sadly the library hasn't been updated since 2010. In the first test run of my code, I received this error:

    RuntimeError: maximum recursion depth exceeded while calling a Python object
    

    Which turns out to be an issue suds has with recursive references on HTTPS connections. See drfence's answer. I had to manually patch suds to get past that issue.

    I switched to php instead. Not as straightforward as python, but I was able to get it working.

    0 讨论(0)
  • 2021-01-29 23:13

    While there isn't a certified standard, if you must use SOAP, Suds is your best choice. Suds can be slow on large WSDLs, and that is something they are working on.

    In the meantime, if you don't expect your WSDL to change often, you have two options that can buy you a lot of speed:

    1. Downloading your WSDL to localhost
    2. Using caching

    Downloading your WSDL

    With large WSDLs part of the problem is that first you must download the WSDL every time, which can add overhead. Suds will take the time to download and parse the entire WSDL on startup to make sure that it hasn't changed.

    If you can download it to the local system and then pass it to the Client constructor using a file:// scheme in the URL. Since Suds uses urllib2 for the HTTP transport, this is perfectly legit.

    Now, because you're not providing a hostname in your WSDL URL, you will also have to pass a location argument specifying the actual URL of the SOAP application.

    Here is an example:

    from suds.client import Client
    
    # The service URL
    soap_url = 'http://myapp.example.notreal/path/to/soap'
    
    # The WSDL URL, we wont' use this but just illustrating for example. This 
    # would be the file you download to your system and save as wsdl_file
    wsdl_url = 'http://myapp.example.notreal/path/to/soap?wsdl' 
    
    # The full path to the downloaded WSDL file on your local system
    wsdl_file = '/path/to/myapp.wsdl'
    wsdl_url = 'file://' + wsdl_file # Override original wsdl_url
    
    client = Client(url=wsdl_url, location=soap_url)
    

    If you're interested, I have used this approach in my work and have open sourced the code.

    Caching your WSDL

    The other option is to use Suds' excellent caching feature. You must explicitly create a cache object and then pass that to the constructor using the cache argument. Otherwise it defaults to an ObjectCache with a duration of 1 day.

    You might also consider using both of these approaches.

    0 讨论(0)
  • 2021-01-29 23:15

    There is new well maintained SOAP client called zeep. It supports both Python 2 and 3 and is based upon well known lxml and requests libraries.

    0 讨论(0)
  • 2021-01-29 23:16

    An interesting up-to-date post can be found here: What SOAP client libraries exist for Python, and where is the documentation for them? Unfortunately, the perfect SOAP library you are looking for seems not to exist (yet)

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