Python web services with SoapLib

I got surprised when I found that there was no Web Services implementation in the Python standard lib but fortunately there is a nice project called SoapLib that is open source and implements web services. SoapLib generates the WSDL for your web service and it is supposed to be ready to consume with Java or .Net.

Service code sample:

  1.  
  2. from soaplib.wsgi_soap import SimpleWSGISoapApp
  3. from soaplib.wsgi_soap import SimpleWSGISoapApp
  4. from soaplib.service import soapmethod
  5. from soaplib.serializers.primitive import *
  6. from cherrypy.wsgiserver import CherryPyWSGIServer
  7.  
  8. class ServersInfoService(SimpleWSGISoapApp):
  9.  
  10. @soapmethod(_returns=Array(String))
  11. def getServersList(self):
  12. results = []
  13. for serverValues in Server.objects.values():
  14. results.append(str(serverValues))
  15. return results
  16.  
  17.  
  18. if __name__=='__main__':
  19. try:
  20. server = CherryPyWSGIServer(('localhost' ,17777), ServersInfoService())
  21. # enable the next two lines if you want HTTPS
  22. #server.ssl_private_key = '/fullpath/file.key'
  23. #server.ssl_certificate = '/fullpath/file.pem'
  24. server.start()
  25. except KeyboardInterrupt:
  26. server.stop()
  27.  

Client code sample:

  1.  
  2. from soaplib.client import make_service_client
  3. import unittest
  4. import os
  5. import re
  6.  
  7. class infoServicesTests(unittest.TestCase):
  8.  
  9. def setUp(self):
  10. # https=True enables HTTPS
  11. self.clientInfoService = make_service_client('localhost:17777/', ServersInfoService(), https=True)
  12.  
  13. def testGetServiceWSDL(self):
  14. # this generates the WSDL:
  15. print self.clientInfoService.server.wsdl('')
  16.  
  17. def testGetServersList(self):
  18. servers = self.clientInfoService.getServersList()
  19. for tuple in servers:
  20. print "Servidor:" + tuple
  21.  

The HTTPS client is still not implemented in SoapLib as far as I know. I've just sent a patch to the SoapLib group that is working for me.
The WSDL generation is made with client.server.wsdl('').
I'll tell you if we can consume the web service from Java successfully.

The Python syntax highlight has been done with CodeHighlighter plugin.

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • some random guy

    Hey there

    Nice post, nice code, and nice blog 🙂

    So, were you able to consume that webservice using java or .net?

    Did you use SoapLib? Did you end using something else?

  • http://carlosble.com Carlos Ble

    Thanks mate!
    I am using SoapLib in my current project and it is working very well. However, it is working with client and server on Python. I am close to try interoperability with .Net but haven’t done yet. I guess I’ll post about that in september when I be back from my holidays. Check out my blog in the near future or send me your email address.
    Cheers

  • http://carloszayas.com Carlos Zayas Guggiari

    Carlos, I tried the client code and it gets me the following error:

    ValueError: no such test method in : runTest

    I will appreciate any help.

  • Manu

    Hi,

    Were u able to consume the Python Web Service in .NET.
    I’m trying a similar thing, and the soap response from the Python Web Service seems to be always getting converted to a null object on my C# client.

  • http://carlosble.com Carlos Ble

    Yes I did. I remeber I had to change the .Net generated proxy but don’t remember what did I do. Try debuging the generated proxy. There is a typecast somewhere which is producing the null object.

    Cheers

  • Francisco

    Hello there. I was trying to develop a soaplib client myself, and all examples I found seem to have the same “akwardness” (at least, for me): In the client code, how are you able to use

    ServersInfoService()

    as a parameter? (I understand that class does not even exists in the client?)

    Thanks in advance!

  • http://www.fitnessmodels.info top fitness models

    I actually have to demonstrate the following to the trainees and check out just what they think about this.

  • http://eforexrobotsblog.info Ernest Gallian

    I think other site proprietors should take this website as an model, very clean and magnificent user genial style and design, as well as the content. You are an expert in this topic!

  • http://www.nauka-jezyka-angielskiego.com.pl/dlaczego-warto-uczyc-sie-angielskiego/ Jezyk angielski

    I like this post, enjoyed this one appreciate it for posting .

  • mohammad hassanzadeh

    very nice tanks,

  • Pingback: Python web services with SoapLib | Currucuxu()