当被调用服务的返回xml内容是按照wsdl文件描述定义的, 就莫名奇妙返回suds.WebFault

没有更多详细信息!

于是将源码解压,并插入到sys.path[0], 通过设置断点的方式找出非标准的返回报文到底在说啥。从而调整对应参数。

suds-0.4/suds/bindings/binding.py (246~268)

    def get_fault(self, reply):
"""
Extract the fault from the specified soap reply. If I{faults} is True, an
exception is raised. Otherwise, the I{unmarshalled} fault L{Object} is
returned. This method is called when the server raises a I{web fault}.
@param reply: A soap reply message.
@type reply: str
@return: A fault object.
@rtype: tuple ( L{Element}, L{Object} )
"""
import pdb
pdb.set_trace()
reply = self.replyfilter(reply)
sax = Parser()
faultroot = sax.parse(string=reply)
soapenv = faultroot.getChild('Envelope')
soapbody = soapenv.getChild('Body')
fault = soapbody.getChild('Fault')
unmarshaller = self.unmarshaller(False)
p = unmarshaller.process(fault)
if self.options().faults:
raise WebFault(p, faultroot)
return (faultroot, p.detail)

顺便记录下suds神器的基本用法,方便查阅:

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:\n', client.last_sent()
print 'last recv:\n', 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='')
(KuaidiQueryResult){
API =
(API){
RESULTS = ""
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

谢谢!

python suds 一坑的更多相关文章

  1. Python 的赋值坑 , a=b=c=1???

    原文地址:https://www.v2ex.com/amp/t/443384 Python 的赋值坑 , a=b=c=1??? 今天回答了一个主题, 一不小心进入了一个坑, 耗费了好多时间终于弄懂了我 ...

  2. 跳出python的各种坑(1)

    2017-11-1915:38:17 一定要跳出python的各种坑,一开始遇到的好多思维上的认知错误,因为刚开始学习,对python是个什么都不清楚,所以记录一下自己遇到的各种坑.不用担心自己遇到的 ...

  3. Python语言防坑小技巧

    Python语言防坑小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值即定义  1>.运行以下代码会出现报错 #!/usr/bin/env python #_*_ ...

  4. python 之走坑的道路

    ### python之 继续走函数的坑 上篇文章简单介绍了函数的一些简单的使用,这次继续踩函数的坑1.函数对象 函数其实也可以当做一个参数传给另一个函数,也可以使用赋值的方式来给另一个,而且这两个的内 ...

  5. 初学python 遇到的坑

    这最近人工智能比较火,看了一下大多都是python的代码,最近看看python 的代码,一出来就遇到了坑,空格的问题先不说了直接上代码吧 # coding = utf-8 import urllib. ...

  6. Python的入坑之路(1)

    (故事背景:由于涉及到机密的原因,暂时不方便透露,待后期再写.) 国庆长假过完之后,回来上班第二天下午,Boss跟龙哥把我叫了出去,问我要不要转人工智能.一脸懵逼的我,带着一脸懵逼听Boss说人工智能 ...

  7. win7 python pdf2image入坑经历

    Python开发菜鸟入坑 项目要求pdf转成图片,网上较多的方案对于windows极其不友好,wand,Pythonmagick(win下载地址:www.lfd.uci.edu/~gohlke/pyt ...

  8. 对Python中一些“坑”的总结及技巧

    一.赋值即定义 1.运行以下代码会出现报错 #!/usr/bin/env python #_*_conding:utf-8_*_ x = 100 def outer(): def inner(): x ...

  9. python socket.io 坑。

    python下star最高的是https://github.com/miguelgrinberg/python-socketio 是flask作者写的.client server都有了,而且还提供了a ...

随机推荐

  1. codeforces 711B - Chris and Magic Square(矩阵0位置填数)

    题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...

  2. 最近这么火的iOS视频直播

    快速集成iOS基于RTMP的视频推流 http://www.jianshu.com/p/8ea016b2720e iOS视频直播初窥:高仿<喵播APP> http://www.jiansh ...

  3. autofac Adding services after container has been built

    http://stackoverflow.com/questions/6173566/run-time-registration-with-autofac Yes you can, using the ...

  4. JS 语言的Function 解析

    1.最基本的作为一个本本分分的函数声明使用. 复制代码代码如下: function func(){} 或 var func=function(){};  2.作为一个类构造器使用: 复制代码代码如下: ...

  5. OC-内存管理-基本原理与引用计数器

    基本原理 1. 什么是内存管理 移动设备的内存极其有限,每个app所能占用的内存是有限制的 当app所占用的内存较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间.比如回收一些不需要使用 ...

  6. 如何在editplus中配置ctags?

    首先要说明的是, 在editPlus中的ctags功能确实是没有 vs vim等中的好用. 最主要的原因 是它不能直接在文件中 跳转. 而是要通过一个另外的框来实现, 这就大大的降低了跳转的速度和使用 ...

  7. Myeclipse如何关联源码

    Myeclipse版本:Myeclipse2014 关联源码前要下载对应的源码,如本例的dom4j-1.6.1.jar,则去下载对应的源码dom4j-1.6.1.zip 如果做的是web项目,就要将该 ...

  8. 包介绍 - UriTemplates (用于处理格式化Uri模板)

    UriTemplates 用于处理格式化Uri模板 PM> Install-Package Tavis.UriTemplates 设置Uri Path Segment [Fact] public ...

  9. spring缓存Ehcache(入门2)

    使用Ehcache缓存工具类. 一.由于使用了maven,所以需要引入依赖包: <dependency> <groupId>net.sf.ehcache</groupId ...

  10. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...