[zz]Python SOAP client with suds

http://www.jansipke.nl/python-soap-client-with-suds

he library suds allows Python to make SOAP calls (that is, Python is the web service client).

We start by installing the suds library on an Ubuntu machine. The Python setuptools are needed to install suds.

sudo apt-get install python-setuptools

Then we download, unpack and install suds.

wget https://fedorahosted.org/releases/s/u/suds/python-suds-0.3.7.tar.gz
tar -zxvf python-suds-0.3.7.tar.gz
cd python-suds-0.3.7
sudo python setup.py install

The library is now ready to use. We start by importing the suds library, creating a client based on a SOAP url, and asking the library to print the SOAP web service methods that are available to us.

import suds
url = "http://www.ecubicle.net/iptocountry.asmx?wsdl"
client = suds.client.Client(url)
print client

From the output of the last print command, we learn that there is a method called FindCountryAsString that takes one argument: the IP address.

print client.service.FindCountryAsString("194.145.200.104")

And it shows (edited for readability):

<?xml version="1.0"?>
<IPAddressService>
   <country>Netherlands</country>
</IPAddressService>

Normally you want to have the contents of the SOAP body. This is what suds provides in a very elegant way. However, you’re a bit stuck when you want to get something from the SOAP header. The author of suds realised this and made a backdoor to get the information anyway. We start by showing what the function last_received contains:

print client.last_received()
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope>
   <soap:Header>
      <ResponseHeader xmlns="">
         <resultCode>1000</resultCode>
         <resultDescription>Success</resultDescription>
      </ResponseHeader>
   </soap:Header>
   <soap:Body>
...
   </soap:Body>
</soap:Envelope>

We can get portions of this data by doing some XML handling. Let’s say we want to print the resultCode:

print client.last_received().getChild("soap:Envelope").getChild("soap:Header").getChild("ResponseHeader").getChild("resultCode").getText()

转载于:https://www.cnblogs.com/bettermanlu/archive/2011/08/05/2128974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值