1. 查看服务接口
import suds
client=suds.client.Client('xxxx_webservice_url')
def get_all_methods(client):
return [method for method in client.wsdl.services[0].ports[0].methods]
2. 查看某个具体接口的传输参数及类型
def get_method_args(client, method_name):
method = client.wsdl.services[0].ports[0].methods[method_name]
input_params = method.binding.input
return input_params.param_defs(method)
3. 调用接口服务
client.service.xxx_function(....)
4. 关于调试
输出之前调用服务接口时发送了什么soap报文,以及收到什么样的soap报文
print 'last sent:
', client.last_sent()
print 'last recv:
', client.last_received()
当服务器返回报文格式不规范时(非wsdl中定义的),client.last_received()为None。这个时候显然对联调极为不利。
那就利用suds自身的日志记录看看咯。设定如下:
import sys
import logging
logger = logging.getLogger('suds')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
如果只关心传输了什么,则可以限定logger为“suds.transport.http“
import sys
logger = logging.getLogger('suds.transport.http')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
5. 异常的捕捉和输出
try:
client.service.xxx_func(*args, **kwargs)
except suds.WebFault,ex:
print ex.fault
print ex.document
后记:对于第三方库莫名其妙的问题,果断及时拿源码过来剖析(至少debug)
DEMO
来个demo说明如何运用,如下:
>>> import suds
>>> url='http://www.gpsso.com/webservice/kuaidi/kuaidi.asmx?wsdl'
>>> client = suds.client.Client(url)
>>> print get_all_methods(client)
[KuaidiQuery]
>>> print get_method_args(client, 'KuaidiQuery')
[(Compay, <Element:0x7f6c55bc43d0 name="Compay" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />), (OrderNo, <Element:0x7f6c55bc4450 name="OrderNo" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />)]
>>> print client.service.KuaidiQuery(Company='EMS', OrderNo='1111')
(KuaidiQueryResult){
API =
(API){
RESULTS = "0"
MESSAGE = "接口查询成功"
}
}
>>> print client.last_sent()
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://gpsso.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:KuaidiQuery>
<ns1:OrderNo>1111</ns1:OrderNo>
</ns1:KuaidiQuery>
</ns0:Body>
</SOAP-ENV:Envelope>
>>> print client.last_received()
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope>
<soap:Body>
<KuaidiQueryResponse xmlns="http://gpsso.com/">
<KuaidiQueryResult>
<API>
<RESULTS>0</RESULTS>
<MESSAGE>接口查询成功</MESSAGE>
</API>
</KuaidiQueryResult>
</KuaidiQueryResponse>
</soap:Body>
</soap:Envelope>
转载请注明本文来源:http://www.cnblogs.com/Tommy-Yu/p/5567091.html