I generally don’t recomend using SOAP instead of REST but I have been required to use SOAP so much now that I think it is inevitably going to be a requirement for a long time for certain projects. So when I noticed a question on stack overflow about using SOAP on the Google App Engine I thought it might be a nice exercise to see how easy it is to get fringe toolsets to work in the GAE.
I’m going to assume basic knowledge of the GAE system. If you haven’t already tried it out you should read the getting started guide. You shouldn’t need to know any more than what is in that guide to work through this example. I also assume knowledge of creating WSDL files by hand. You will need to be able to do this to generate the SOAP server and client. You can find a WSDL tutorial here that may help. I also recommend creating an empty directory that will house your project for this example.
The first step involves getting a library from the Python Web Services project. You will want to download the ZSI 2.1a1 package (grab the the tgz file: ZSI-2.1-a1.tar.gz). This is the current alpha release of ZSI but the current stable release won’t work. The stable release has a problem with the expat library but the problem is fixed in 2.1. It is also worth stating that ZSI 2.1 seems to be better put together. Extract the files into a temp directory and then copy only the directory named “ZSI” into your project directory.
Next you will need to get zope.interface. Go to the zope.interface download page and find the file named: zope.interface-3.5.0.zip. Unzip this file in a temp directory and then copy the directory named “zope” into your project.
These two external libraries are all you need to create a Python SOAP service. The next step in creating the service involves using the GAE webapp framework so it would help to read up on that. This framework works with WSGI and it may also help to learn about WSGI as well. I referenced the django example and example of using routes to get an idea of what needed to be done to connect the GAE WSGI up to the ZSI SOAP service. They both use the WSGIApplication webapp call to tie in their output with the WSGI framework and that is common to all applications that use the webapp framework.
Now that all the libraries are in place and the WSGI and GAE webapp interface make some sense it is time to move on to creating a WSDL file. Here is a simple one that I used for the test:
[code lang=”xml”]
[/code]
Notice at the bottom I set the address to the location of my test service along with the application domain name. This is where the application will end up in production. If you want to test first you can stick your localhost information in there instead.
Next you will need to run the wsdl2py application that comes with ZSI on your WSDL file to generate the classes needed for the server. To do this I simply copied the wsdl2py application into the project and ran the following command:
[code lang=”text”]
python wsdl2py SimpleEcho.wsdl
[/code]
After you do this you will find a couple new files in your project directory. I then removed the wsdl2py application so it didn’t get uploaded later.
Next create the test endpoint application itself. For this example I named the file echo.py and added the hooks that connect the service to the WSGI interface:
[code lang=”python”]
import sys
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from EchoServer_server import *
from ZSI.twisted.wsgi import SOAPApplication, soapmethod, SOAPHandlerChainFactory, WSGIApplication
class EchoService(SOAPApplication):
factory = SOAPHandlerChainFactory
wsdl_content = dict(name=’Echo’, targetNamespace=’urn:echo’, imports=(), portType=”)
@soapmethod(EchoRequest.typecode, EchoResponse.typecode, operation=’Echo’, soapaction=’Echo’)
def soap_Echo(self, request, response, **kw):
response = request
return request,response
application = WSGIApplication()
application[‘echo’] = EchoService()
def main():
run_wsgi_app(application)
if __name__ == “__main__”:
main()
[/code]
Next create the application description file app.yaml that is needed to upload and install the application in the GAE. For simplicity I just map everything to the main program:
[code lang=”text”]
application: iechotest
version: 1
runtime: python
api_version: 1
handlers:
– url: /.*
script: echo.py
[/code]
At this point my project directory looked like this:
[code lang=”text”]
soaptest/
ZSI/
zope/
app.yaml
EchoServer_client.py
EchoServer_types.py
EchoServer_server.py
echo.py
SimpleEcho.wsdl
[/code]
The application is ready to be uploaded to GAE at this point. After uploading you can stick the endpoint URL into a browser and you should see a message saying “NO RESOURCE FOR GET”. If you get some type of error use the logs in your application console to look at the application stack trace. If there are no errors you are ready to build a client to test the SOAP service.
To create the client you use the same WSDL file with wsdl2py again. This actually generates the same class files and you need the same libraries in place to run it. The following example client will test the service:
[code lang=”python”]
from EchoServer_client import *
import sys, time
TRACE=None
loc = EchoServerLocator()
port = loc.getEchoServer()
msg = EchoRequest()
msg._value = 1
rsp = port.Echo(msg)
print “INTEGER: “, rsp._value
msg._value = “HI”
rsp = port.Echo(msg)
print “STRING: “, rsp._value
msg._value = 1.10000
rsp = port.Echo(msg)
print “FLOAT: “, rsp._value
msg._value = dict(milk=dict(cost=3.15, unit=”gal”))
[/code]
As long as the service is in the same place as the WSDL location setting you don’t need to specify an endpoint for the getServer call.
And that is all there is to it. It is fairly involved but if you already understand some of the concepts here it seems fairly strait forward. Along the way I found a SOAP tutorial for Python that may be helpful as well.
Awesome, awesome, awesome.
Thank you for sharing this.
This was a great tutorial! Just a note to other readers if you try and use zope-interface-3.5.1 (the current version) you’ll have problems because of dependencies on setuptools (which is problematic in google appengine’s environment)…. I recommend you stick with 3.5.0 to work through this example.
Thanks again!
Hi there!
Thank you for your sample. It was really useful for me. But I have a problem with array type. can you please help me? how to return an array data. I try this:
with GetValueResponse is an array. And i got this error:
ArrayOfGetValueResult_Holder instance has no attribute ‘_sourceid’
What should i do? please help me…
kien vu,
It doesn’t look like you are incrementing your index when you iterate over v2. My guess would be that the result isn’t built like you think it should be.
Thanks for you great soap tutorial. I follow your tutorial and everything works great. Howerver, my project need to build soap client in window mobile device and connect to the cloud. According to MSDN , it indicates that “The .NET Compact Framework does not support all the code generated by the Web Services Description Language Tool (Wsdl.exe). However, applications that use Web services can use the generated proxy when you add a Web reference to a Smart Device project in Visual Studio.” As a result, I need to locate the wsdl file that I upload in my web service, right ? Please help me about how to locate my wsdl by trying http://memory-saver.appspot.com/echo?wsdl (“echo” is my operation), but it comes out some weird wsdl stuff. Thank you for reading my comment. I am really appreciate your help.
I’m able to get as far as the “No resource for get” step but run into trouble after that. Am I supposed to actually run the wsdl2py for a second time and overwrite the previously generated files? I’ve created the example client to test the service but still get the “no resource for get” message. Do I have to create a new mapping for the example client? Thanks for any help
Got an error in the echo.py file, it was missing a ” on line 9,
portType=””)
worked, but as copied from the website it was like this
portType=”) <– error
and the wsdl2py had to be renamed with a .py extension and then it worked, at least in how i used it with dos, may not have put the python at the front after thinking about it…
and what it says is nothing to see hear? where does this come from? how to add a button and text box to echo what i send it? or etc?
thanks
Ok just noticed there was one more set of code to copy and try, it reads:
To create the client you use the same WSDL file with wsdl2py again. This actually generates the same class files and you need the same libraries in place to run it. The following example client will test the service:
but what should that code be named? if I run the same wsdl file and same wsdl2py again the code below this never gets used. It appears to be a .py file, i named it client.py and ran client.py bradz.wsdl file and it got 4 errors:
C:\Users\myfiles\bradley\bradley>client.py bradz.wsdl <–in DOS cmd shell
Traceback (most recent call last):
File "C:\Users\myfiles\bradley\bradley\client.py", line 9, in
rsp = port.Echo(msg)
File “C:\Users\myfiles\bradley\bradley\EchoServer_client.py”, line 40, in Echo
response = self.binding.Receive(EchoResponse.typecode)
File “C:\Users\myfiles\bradley\bradley\ZSI\client.py”, line 531, in Receive
self.ReceiveSOAP(**kw)
File “C:\Users\myfiles\bradley\bradley\ZSI\client.py”, line 416, in ReceiveSOAP
‘Response is “%s”, not “text/xml”‘ % self.reply_headers.type)
TypeError: Response is “text/plain”, not “text/xml”
Could you please clarify a bit on this step?