当被调用服务的返回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. App Store

    App store最新审核标准(2015.3)公布 http://www.cnii.com.cn/mobileinternet/2015-03/24/content_1550301.htm iOS提交 ...

  2. Django笔记-字符编码相关问题整理

    1.添加中文注释后编译出错,提示:Non-ASCII   解决方法: 在Python脚本文件的第一行或第二行添加一句:      #coding:gbk或#coding:utf-8或##-*- cod ...

  3. Java并发编程核心方法与框架-Semaphore的使用

    Semaphore中文含义是信号.信号系统,这个类的主要作用就是限制线程并发数量.如果不限制线程并发数量,CPU资源很快就会被耗尽,每个线程执行的任务会相当缓慢,因为CPU要把时间片分配给不同的线程对 ...

  4. [Bug]IIs Cannot read configuration file due to insufficient permissions

    摘要 在部署站点的时候,遇到这样的问题Cannot read configuration file due to insufficient permissions 解决办法 在服务器上部署站点,浏览的 ...

  5. firefox如何卸载插件plugins和临时文件夹

    下载原版的 英文版的 firefox 会看到 openH264 video codec Plugin和microsoft DRM (digit rightcopy manager 数字版权管理)等等插 ...

  6. php 选择排序法

    private function demo(array $arr = array()){ $len = count($arr); if ($len == 1) return $arr; else fo ...

  7. Memcached目录

    Memcached 简介.安装和基本使用 Memcached基础知识 理解Memcached的分布式 Memcached存储命令 - set Memcached存储命令 - add Memcached ...

  8. HTTP协议详解(真的很经典)

    HTTP 是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和 扩展.目前在WWW中使用的是HTTP/1.0 ...

  9. PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)

    原文: PHP5: mysqli 插入, 查询, 更新和删除  Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...

  10. google-breakpad

    BUG收集系统: 国内可以使用友盟,国外可以使用Crashlytics SDK,或者自己实现使用 google-breakpad,参见下列文章: Crashlytics SDK 部分: http:// ...