SOAP (or Simple Object Access protocol) is a protocol that is used for development of web services in turn used for exchanging information across computers. While the message format is XML, the mode of negotiation and transmission typically is HTTP. SOAP provides a messaging framework where the client and web services are bind to a protocol and message format. More about SOAP can be read at wikipedia..
SOAP web services can be developed on top of web servers like Apache (*NIX) or IIS (Windows).. and of course Tornado web server! Let’s see how…
rancavil has come up with an excellent module that helps you develop SOAP based web services in Python and Tornado. This post describes development of SOAP Web Service on Tornado and a client program that can access this web service using Python-suds.
Let’s take an example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.httpserver | |
import tornado.ioloop | |
from tornadows import soaphandler | |
from tornadows import webservices | |
from tornadows import xmltypes | |
from tornadows.soaphandler import webservice | |
class MathService(soaphandler.SoapHandler): | |
@webservice(_params=[int,int],_returns=int) | |
def add(self, a, b): | |
return a+b | |
if __name__ == '__main__': | |
service = [('MathService',MathService)] | |
ws = webservices.WebService(service) | |
application = tornado.httpserver.HTTPServer(ws) | |
application.listen(8080) | |
tornado.ioloop.IOLoop.instance().start() |
In this code snippet:
- We create a SOAP Handler ‘MathService’ that performs mathematical operations
- Method add() is defined that tales two integers as input and returns an integer value
- Run the python code and the web service is up and running…
- You could browse the WSDL at http://localhost:8080/MathService?wsdl
Let’s now have a look at the client code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import suds | |
## Invoke add method of MathService | |
url = 'http://localhost:8080/MathService?wsdl' | |
client = suds.client.Client(url,cache=None) | |
print client.service.add(2,3) | |
print "Request sent" | |
print client.last_sent() | |
print "Response received" | |
print client.last_received() |
- The Client is developed with Python Suds module
- It makes a request to the add() method of MathService with parameters 2 and 3
- and waits for a response
Output of this code can be found below, where the response body contains the summation of 2 and 3 i.e., 5.
5
Request sent
<!--?xml version="1.0" encoding="UTF-8"?>
xmlns:ns0="http://127.0.1.1:8080/MathService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns0:paramsadd>
<a>2</a>
<b>3</b>
<!--ns0:paramsadd>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Response received
<!--?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<returns>5</returns>
<!--soapenv:Body>
<!--soapenv:Envelope>