Beardy Geek – Web Development Blog

A blog about web development – giving tips and tutorials about all aspects of web dev.

Beardy Geek – Web Development Blog

How to Call a .Net Webservice using Python

My day job involves working with software that automatically creates webservices on the .Net platform. Up until now I have used C# to create web applications to use these webservices.

But it would be nice to have the flexibility to use another programming language to create a solutions, and consume the webservices from there.

So, here’s a short tutorial on calling your .net webservice using Python.


  1. I’m currently using Python 2.5, so I can’t speak for other versions
  2. You will need the ElementSoap package from You can get it from here
  3. You’re also going to need a .Net webservice with which to test this out. If you’re reading this tutorial, you probably already have one.


Firstly we need to look at the SOAP examples on your .Net webservice. Go your webservice’s asmx page, and then click on one of your webservices to view the details. You will see some example SOAP requests and responses. The one we’re interested in here is SOAP 1.1. Mine looks like this:

POST /lookserver/webservices.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: ""

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi=""
    <getVersion xmlns="" />

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi=""
    <getVersionResponse xmlns="">

As we can see here, I have a webservice called GetVersion, the namespace is, and the SOAPAction is Note these down for your webservice.

Into the Python

Fire up an interactive session, and we’ll go through calling this service.

Firstly we need to import the ElementSOAP library:

from elementsoap import ElementSOAP as ES

If you get an error message, then you haven’t installed ElementSoap properly.

Next we create a SoapRequest:

sr = ES.SoapRequest("{}GetVersion")

And now we initialize a SoapService object:

serv = ES.SoapService("http://localhost/lookserver/webservices.asmx")

Obviously put in your own url for your webservice.

Now we call the webservice, using the SOAPAction value:

result ="", sr)

The result is an xml element which should contain the response from your webservice. When I type result I get:

<element '{}GetVersionResponse' at 00A1A8A8>

If you look at the xml at the top of the article, you’ll see that the result returns inside a GetVersionResponse tag. To get the value of the result we type:


This finds the appropriate tag, and returns the text.



I hope this has helped. When I searched, I found that the other tutorials were either confusing, or out of date. Take a look at the various libraries at, especially ElementTree for parsing XML, very useful. Enjoy!