python - SUDS - 以编程方式访问方法和类型

标签 python suds

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

目的是生成用户界面,允许用户选择方法,然后在动态生成的表单中填写值。

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

client = Client(url)
method = client.sd.service.methods['MyMethod']

我无法以编程方式弄清楚我需要创建什么对象类型才能调用服务

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

res = client.service.MyMethod(obj, soapheaders=authen)

有人有示例代码吗?

最佳答案

好的,所以 SUDS 确实很神奇。

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

client = suds.client.Client("http://mssoapinterop.org/asmx/simple.asmx?WSDL")

它下载 WSDL 并在 client.wsdl 中创建定义.当您通过 client.service.<method> 调用使用 SUDS 的方法时它实际上在幕后针对解释的 WSDL 执行了大量递归解析魔术。要发现方法的参数和类型,您需要自省(introspection)此对象。

例如:

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

这应该打印如下内容:

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)

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

>>> client.factory.create(u'ArrayOfInt')
(ArrayOfInt){
   _arrayType = ""
   _offset = ""
   _id = ""
   _href = ""
   _arrayType = ""
 }

更新:

对于 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
 }

但这在方法调用的参数中很神奇(a la client.service.GetWeatherByZipCode("12345")。IIRC 这是 SOAP RPC 绑定(bind)样式?我认为这里有足够的信息可以帮助您入门。提示:Python 命令行界面是你的 friend !

关于python - SUDS - 以编程方式访问方法和类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/241892/

相关文章:

python - SUDS:在 Client.service 方法中传递数组参数:GAE Python

Python SOAP 客户端 - 使用 SUDS 还是其他?

python - 将 Suds SOAP 复杂数据类型解析为 Python 字典

python - 尝试从低级套接字服务器发送 HTTP 响应

python - 如何在昂贵的导入之前优雅地解析 python 中的参数?

python - 除了Python中的中断

Python枚举元使打字模块崩溃

Python 泡沫显示以下问题 "RuntimeError: maximum recursion depth exceeded"

python - 如何使用 Python suds 解析 wsdl 的 xml 内容?

python字符串格式,负号表示负数,但空格表示正数