<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Beardy Geek &#187; Python</title>
	<atom:link href="http://www.beardygeek.com/category/web-development/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.beardygeek.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Fri, 30 Jul 2010 21:48:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Call a .Net Webservice using Python</title>
		<link>http://www.beardygeek.com/2009/01/how-to-call-a-net-webservice-using-python/</link>
		<comments>http://www.beardygeek.com/2009/01/how-to-call-a-net-webservice-using-python/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 12:51:46 +0000</pubDate>
		<dc:creator>BeardyGeek</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://www.beardygeek.com/?p=8</guid>
		<description><![CDATA[Learn how to consume a .net webservice using Python]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>But it would be nice to have the flexibility to use another programming language to create a solutions, and consume the webservices from there.</p>
<p>So, here&#8217;s a short tutorial on calling your .net webservice using Python.</p>
<p>
<h4>Pre-requisites</h4>
<ol>
<li>I&#8217;m currently using Python 2.5, so I can&#8217;t speak for other versions</li>
<li>You will need the ElementSoap package from effbot.org.  <a href="http://effbot.org/downloads/#elementsoap" onclick="pageTracker._trackPageview('/outgoing/effbot.org/downloads/_elementsoap?referer=');">You can get it from here</a></li>
<li>You&#8217;re also going to need a .Net webservice with which to test this out. If you&#8217;re reading this tutorial, you probably already have one.</li>
</ol>
<p>
<h4>SOAP</h4>
<p>Firstly we need to look at the SOAP examples on your .Net webservice.  Go your webservice&#8217;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&#8217;re interested in here is SOAP 1.1.  Mine looks like this:</p>
<pre>
<code>
POST /lookserver/webservices.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetVersion"

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;soap:Body&gt;
    &lt;getVersion xmlns="http://tempuri.org/" /&gt;
  &lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;

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

&lt;xml version="1.0" encoding="utf-8"?&gt;
&lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;soap:Body&gt;
    &lt;getVersionResponse xmlns="http://tempuri.org/"&gt;
      &lt;getVersionResult&gt;string&lt;/getVersionResult&gt;
    &lt;/getVersionResponse&gt;
  &lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;
</code>
</pre>
<p>As we can see here, I have a webservice called GetVersion, the namespace is http://tempuri.org, and the SOAPAction is http://tempuri.org/GetVersion.  Note these down for your webservice.
</p>
<p>
<h4>Into the Python</h4>
<p>Fire up an interactive session, and we&#8217;ll go through calling this service.</p>
<p>Firstly we need to import the ElementSOAP library:</p>
<pre>
<code>from elementsoap import ElementSOAP as ES</code>
</pre>
<p>
</p>
<p>If you get an error message, then you haven&#8217;t installed ElementSoap properly.
</p>
<p>Next we create a SoapRequest:</p>
<pre>
<code>sr = ES.SoapRequest("{http://tempuri.org/}GetVersion")</code>
</pre>
<p></p>
<p>And now we initialize a SoapService object:</p>
<pre>
<code>serv = ES.SoapService("http://localhost/lookserver/webservices.asmx")</code>
</pre>
<p></p>
<p>Obviously put in your own url for your webservice.</p>
<p>Now we call the webservice, using the SOAPAction value:</p>
<pre>
<code>result = serv.call("http://tempuri.org/GetVersion", sr)</code>
</pre>
<p></p>
<p>The result is an xml element which should contain the response from your webservice. When I type result I get:</p>
<pre>
<code>&lt;element '{http://tempuri.org/}GetVersionResponse' at 00A1A8A8&gt;</code>
</pre>
<p></p>
<p>If you look at the xml at the top of the article, you&#8217;ll see that the result returns inside a GetVersionResponse tag.  To get the value of the result we type:</p>
<pre>
<code>result.find("{http://tempuri.org/}GetVersionResult").text</code>
</pre>
<p></p>
<p>This finds the appropriate tag, and returns the text.
</p>
<p>&nbsp;</p>
<h4>Conclusion</h4>
<p>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 effbot.org, especially ElementTree for parsing XML, very useful.  Enjoy!</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.beardygeek.com/2009/01/how-to-call-a-net-webservice-using-python/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
