# How to create a Python class from a SOAP WSDL

## Using Python and <span class="caps">SOAP</span> to attach to a Web Service

You can create a Python class from a <span class="caps">SOAP</span> <span class="caps">WSDL</span> in 4 steps, in about 20 minutes.

1. Create your Python Project
2. Download and install a <span class="caps">SOAP</span> library for Python (this how-to will use ZSI’s soap library)
3. Create a base Python class from a <span class="caps">WSDL</span> (either a file or a <span class="caps">URL</span>)
4. Tweak it for your purposes!

## Step One – Create your Python Class

This might be the easiest step of all- if you’re just using Python to create an application, then you’re set! If you’re using something fancier, such as a web application framework, there might some extra steps involved.

For a simple, vanilla python example, just create yourself a directory (let’s call it soapclient) where you can write files and access python.exe (or just python in a unix/unix-like system), and navigate to that directory.

## Step Two – Download and install a <span class="caps">SOAP</span> library for Python

Here, I’m listing the steps for the <span class="caps">ZSI</span> (Zolera Soap Infrastructure) libraries- it has more comprehensive support for WSDL’s than the other Python <span class="caps">SOAP</span> libraries. If you’re in a hurry, the quick way is to use something like [EasyInstall](http://peak.telecommunity.com/DevCenter/EasyInstall) from <span class="caps">PEAK</span> (Python Enterprise Application Kit). <span class="caps">NOTE</span>: Currently, EasyInstall provides the production version of <span class="caps">ZSI</span>, which is 2.0rc3. If you’re using a modern version of Python, with libraries provided by either ActiveState or OS X, you’ll have problems due to dependencies. <span class="caps">ZSI</span> 2.0rc3 is based on the PyXML libraries, which are no longer maintained and conflict with those provided by packaged Python releases. If you are using a pre-installed or packaged version of Python, you might need to use the beta, <span class="caps">ZSI</span> 2.1a1, which is based on minidom and compatible with packaged Python releases. Eventually, this should work itself out!

To install from the web using EasyInstall, follow the directions to install EasyInstall from the <span class="caps">PEAK</span> website, then just type “easy\_install <span class="caps">ZSI</span>”. If you’re installing <span class="caps">ZSI</span> by hand, download the tar.gz, and do the “configure”, “make”, “make install”.

Using EasyInstall to install the <span class="caps">ZSI</span> 2.1a1 release, you’ll need to just download the egg from sourceforge for your version of Python, then type “easy\_install <span class="caps">ZSI</span>-2.1\_a1-py2.5.egg”, using Python 2.5 as an example.

## Step Three – Create a base Python class from a <span class="caps">WSDL</span>

Now, here’s the fun part! In a unix/unix-ish system, <span class="caps">ZSI</span> will install a small script file called wsld2py in your /usr/local/bin directory, or a wsdl2py.exe in your ActiveState Scripts directory. Using that, type “wsdl2py ”.

Let’s use the [Braille Text Service](http://www.xmethods.com/ve2/ViewListing.po?key=uuid:A83A6AD1-A986-9321-0179-866A16700722) from xmethods as an example, to find the Braille equivalent of <span class="caps">UCLA</span>.

wsdl2py —complexType “[http://www.webservicex.net/braille.asmx](http://www.webservicex.net/braille.asmx)?<span class="caps">WSDL</span>”  
(no semi-colon, though.. that’s just a display issue in this entry)

Using <span class="caps">ZSI</span> 2.1, this will give you a couple of classes: ndfdXML\_client.py, ndfdXML\_server.py, and ndfdXML\_types.py. Using <span class="caps">ZSI</span> 2.0, you’ll get a ndfdXML\_service.py and a ndfdXML\_types.py file.

## Step Four – Tweak it for your purposes

Now I’m going to focus on <span class="caps">ZSI</span> 2.1, because it’s easier. (sorry!)

From a command line, you can start python, then you’ll get the “&gt;&gt;&gt;” prompt.

Here’s the code to get the Braille text for <span class="caps">UCLA</span> (you just need the parts after the “&gt;&gt;&gt;”):  
&gt;&gt;&gt; from Braille\_client import \*  
&gt;&gt;&gt; mylocator = BrailleLocator()  
&gt;&gt;&gt; myport = mylocator.getBrailleSoap()  
&gt;&gt;&gt; myrequest = BrailleTextSoapIn()  
&gt;&gt;&gt; myrequest.InText = “<span class="caps">UCLA</span>”  
&gt;&gt;&gt; myrequest.TextFontSize = 24  
&gt;&gt;&gt; myresponse = myport.BrailleText(myrequest)  
&gt;&gt;&gt; f = open(‘uclabraille.jpg’, ‘w’)  
&gt;&gt;&gt; f.write(myresponse.BrailleTextResult)  
&gt;&gt;&gt; f.close()  
&gt;&gt;&gt; quit()

With any luck, you’ll get a file named uclabraille.jpg in your project directory, with an image of <span class="caps">UCLA</span> in braille. Voila! You’re done!

If you’re working with your own web services, you’ll need to find what the request parameters and return parameters are… the easiest method I’ve found so far is to use the python prompt, make your requests or responses, and type “print dir(myrequest)”, where myrequest is the name of your request object! Then it’s a little bit of trial and error.

Next steps: Next, you’d want to incorporate that in something like a compiled python app if you’re building a gui, or into a website if you’re building a python based website!