SUDS - 对方法和类型的编程访问

GHZ*_*GHZ 21 python suds

我正在调查SUDS作为python的SOAP客户端.我想检查指定服务中可用的方法,以及指定方法所需的类型.

目的是生成用户界面,允许用户选择方法,然后以动态生成的形式填写值.

我可以获得有关特定方法的一些信息,但我不确定如何解析它:

client = Client(url)
method = client.sd.service.methods['MyMethod']
Run Code Online (Sandbox Code Playgroud)

我无法编程以 确定我需要创建哪种对象类型才能调用该服务

obj = client.factory.create('?')

res = client.service.MyMethod(obj, soapheaders=authen)
Run Code Online (Sandbox Code Playgroud)

有没有人有一些示例代码?

sj2*_*j26 27

好的,所以SUDS确实有点神奇.

A suds.client.Client,是从WSDL文件构建的:

client = suds.client.Client("http://mssoapinterop.org/asmx/simple.asmx?WSDL")
Run Code Online (Sandbox Code Playgroud)

它下载WSDL并在其中创建定义client.wsdl.当你使用SUDS调用一个方法时,client.service.<method>它实际上在幕后对着解释的WSDL进行了大量的递归解析.要发现方法的参数和类型,您需要内省此对象.

例如:

for method in client.wsdl.services[0].ports[0].methods.values():
    print '%s(%s)' % (method.name, ', '.join('%s: %s' % (part.type, part.name) for part in method.soap.input.body.parts))
Run Code Online (Sandbox Code Playgroud)

这应该打印如下:

echoInteger((u'int', http://www.w3.org/2001/XMLSchema): inputInteger)
echoFloatArray((u'ArrayOfFloat', http://soapinterop.org/): inputFloatArray)
echoVoid()
echoDecimal((u'decimal', http://www.w3.org/2001/XMLSchema): inputDecimal)
echoStructArray((u'ArrayOfSOAPStruct', http://soapinterop.org/xsd): inputStructArray)
echoIntegerArray((u'ArrayOfInt', http://soapinterop.org/): inputIntegerArray)
echoBase64((u'base64Binary', http://www.w3.org/2001/XMLSchema): inputBase64)
echoHexBinary((u'hexBinary', http://www.w3.org/2001/XMLSchema): inputHexBinary)
echoBoolean((u'boolean', http://www.w3.org/2001/XMLSchema): inputBoolean)
echoStringArray((u'ArrayOfString', http://soapinterop.org/): inputStringArray)
echoStruct((u'SOAPStruct', http://soapinterop.org/xsd): inputStruct)
echoDate((u'dateTime', http://www.w3.org/2001/XMLSchema): inputDate)
echoFloat((u'float', http://www.w3.org/2001/XMLSchema): inputFloat)
echoString((u'string', http://www.w3.org/2001/XMLSchema): inputString)
Run Code Online (Sandbox Code Playgroud)

因此,部件类型元组的第一个元素可能就是你所追求的:

>>> client.factory.create(u'ArrayOfInt')
(ArrayOfInt){
   _arrayType = ""
   _offset = ""
   _id = ""
   _href = ""
   _arrayType = ""
 }
Run Code Online (Sandbox Code Playgroud)

更新:

对于Weather服务,似乎"参数"是element不具有type以下内容的部分:

>>> client = suds.client.Client('http://www.webservicex.net/WeatherForecast.asmx?WSDL')
>>> client.wsdl.services[0].ports[0].methods.values()[0].soap.input.body.parts[0].element
(u'GetWeatherByZipCode', http://www.webservicex.net)
>>> client.factory.create(u'GetWeatherByZipCode')
(GetWeatherByZipCode){
   ZipCode = None
 }
Run Code Online (Sandbox Code Playgroud)

但这对于方法调用的参数来说是神奇的(la.IIRC client.service.GetWeatherByZipCode("12345")这是SOAP RPC绑定样式吗?我认为这里有足够的信息可以帮助你开始.提示:Python命令行界面是你的朋友!


art*_*nil 19

根据suds 文档,您可以检查service对象__str()__.以下是获取方法和复杂类型的列表:

from suds.client import Client;

url = 'http://www.webservicex.net/WeatherForecast.asmx?WSDL'
client = Client(url)

temp = str(client);
Run Code Online (Sandbox Code Playgroud)

上面的代码产生以下结果(内容temp):

Suds ( https://fedorahosted.org/suds/ )  version: 0.3.4 (beta)  build: R418-20081208

Service ( WeatherForecast ) tns="http://www.webservicex.net"
   Prefixes (1)
      ns0 = "http://www.webservicex.net"
   Ports (2):
      (WeatherForecastSoap)
         Methods (2):
            GetWeatherByPlaceName(xs:string PlaceName, )
            GetWeatherByZipCode(xs:string ZipCode, )
         Types (3):
            ArrayOfWeatherData
            WeatherData
            WeatherForecasts
      (WeatherForecastSoap12)
         Methods (2):
            GetWeatherByPlaceName(xs:string PlaceName, )
            GetWeatherByZipCode(xs:string ZipCode, )
         Types (3):
            ArrayOfWeatherData
            WeatherData
            WeatherForecasts
Run Code Online (Sandbox Code Playgroud)

这将更容易解析.此外,还列出了每个方法及其参数及其类型.您可能甚至可以使用正则表达式来提取所需的信息.


gbu*_*ler 8

这是我根据上述信息编写的快速脚本,列出了WSDL上可用的输入方法suds报告.传入WSDL URL.适用于我目前正在进行的项目,我无法保证为您服务.

import suds

def list_all(url):
    client = suds.client.Client(url)
    for service in client.wsdl.services:
        for port in service.ports:
            methods = port.methods.values()
            for method in methods:
                print(method.name)
                for part in method.soap.input.body.parts:
                    part_type = part.type
                    if(not part_type):
                        part_type = part.element[0]
                    print('  ' + str(part.name) + ': ' + str(part_type))
                    o = client.factory.create(part_type)
                    print('    ' + str(o))
Run Code Online (Sandbox Code Playgroud)