您的位置:首页 > 编程语言 > Python开发

[zz]Python SOAP client with suds

2011-08-05 19:47 441 查看
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()

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: