Welcome, guest | Sign In | My Account | Store | Cart

This reciepe shows how to call a web service located on a remote machine to do stuff (convert centigrads to faranheit and farahneits to centigrads and retrieve the results as a python object.

Python, 27 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#import the WSDL module, this does all the work for you.
from SOAPpy import WSDL

#specify the wsdl file. This file contains everything an application needs to know
#to call the service with the right arguments, with the right protocol at the right
#location etc.
WSDLFile   = "http://developerdays.com/cgi-bin/tempconverter.exe/wsdl/ITempConverter"

#Create a proxy. You can call methods that are on a distant machine as if they were
#on your local machine, as if they were implemented in the proxy object.

proxy      = WSDL.Proxy(WSDLFile)
#uncomment thoses lines to see outgoing and incoming soap envelops
#proxy.soapserver.config.dumpSOAPIn=1
#proxy.soapserver.config.dumpSOAPOut=1

Centigrades = 12

#Here we call the action CtoF as if were a method defined in the proxy object.
Faranheites  = proxy.CtoF(Centigrades)

print "%s Centigrades = %s Faranheites" % (Centigrades,Faranheites)

## trace :
## chaouche@CAY:~/TEST$ python soapCentigradesToFaranheites.py
## 12 Centigrades = 32 Faranheites
## chaouche@CAY:~/TEST$

You could use directly SOAPProxy, but wsdl.Proxy is more convenient since the WSDL file contains both the service URL and the namespace, so you don't have to look for it yourself. Creating the WSDL.Proxy object downloads the WSDL file, parses it, and configures a SOAPProxy object that it uses to call the actual SOAP web service. If there are errors on the wsdl file, an error is raised to warn you that the file is not ok.

All you have to do is to look for the available methods, using for example proxy.methods.keys(), and the method's arguments using proxy.methods["methodName"].inparams which is a list like this : [dic1,dic2,dic3] where dici is like : {"name":"foo","type":"< SomeType >"}

which reads : the first argument is foo and its type is < SomeType >

In our example, here's how it looks like :

>>> proxy.methods.keys()
[u'FtoC', u'CtoF']



>>> proxy.methods['CtoF'].inparams
[< SOAPpy.wstools.WSDLTools.ParameterInfo instance at 0xb78f672c >]
>>> proxy.methods['CtoF'].inparams[0]
< SOAPpy.wstools.WSDLTools.ParameterInfo instance at 0xb78f672c >
>>> proxy.methods['CtoF'].inparams[0].name
u'temp'
>>> proxy.methods['CtoF'].inparams[0].type
(u'http://www.w3.org/2001/XMLSchema', u'int')
>>>

The first argument is "temp" and its type is "int".

The return value is retrieved like this :

>>> proxy.methods['CtoF'].retval.name
u'return'
>>> proxy.methods['CtoF'].retval.type
(u'http://www.w3.org/2001/XMLSchema', u'int')
>>>

references : dive into python, chapter 12.

Created by chaouche yacine ahmed on Mon, 26 Feb 2007 (PSF)
Python recipes (4591)
chaouche yacine ahmed's recipes (1)

Required Modules

Other Information and Tasks