tags:

views:

242

answers:

2

I need to implement a small test utility which consumes extremely simple SOAP XML (HTTP POST) messages. This is a protocol which I have to support, and it's not my design decision to use SOAP (just trying to prevent those "why do you use protocol X?" answers)

I'd like to use stuff that's already in the basic python 2.6.x installation. What's the easiest way to do that? The sole SOAP message is really simple, I'd rather not use any enterprisey tools like WSDL class generation if possible.

I already implemented the same functionality earlier in Ruby with just plain HTTPServlet::AbstractServlet and REXML parser. Worked fine.

I thought I could a similar solution in Python with BaseHTTPServer, BaseHTTPRequestHandler and the elementree parser, but it's not obvious to me how I can read the contents of my incoming SOAP POST message. The documentation is not that great IMHO.

+3  A: 

You could write a WSGI function (see wsgiref) and parse inside it an HTTP request body using the xml.etree.ElementTree module.

SOAP is basically very simple, I'm not sure that it deserves a special module. Just use a standard XML processing library you like.

Andrey Vlasovskikh
Thanks, I'll look into WSGI API. It rings a bell, I might have some Google App Engine code lying around which parses the contents of a POST request. It'll help if App engine's APIs were WSGI-based as well.
auramo
+1  A: 

I wrote something like this in Boo, using a .Net HTTPListener, because I too had to implement someone else's defined WSDL.

The WSDL I was given used document/literal form (you'll need to make some adjustments to this information if your WSDL uses rpc/encoded). I wrapped the HTTPListener in a class that allowed client code to register callbacks by SOAP action, and then gave that class a Start method that would kick off the HTTPListener. You should be able to do something very similar in Python, with a getPOST() method on BaseHTTPServer to:

  • extract the SOAP action from the HTTP headers
  • use elementtree to extract the SOAP header and SOAP body from the POST'ed HTTP
  • call the defined callback for the SOAP action, sending these extracted values
  • return the response text given by the callback in a corresponding SOAP envelope; if the callback raises an exception, catch it and re-wrap it as a SOAP fault

Then you just implement a callback per SOAP action, which gets the XML content passed to it, parses this with elementtree, performs the desired action (or mock action if this is tester), and constructs the necessary response XML (I was not too proud to just create this explicitly using string interpolation, but you could use elementtree to create this by serializing a Python response object).

It will help if you can get some real SOAP sample messages in order to help you not tear out your hair, especially in the part where you create the necessary response XML.

Paul McGuire
Boo/.Net is not exactly Python :-) My problem is not conceptual, it's getting the exact implementation working. I also already have a working Ruby implementation.
auramo