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:
from soaplib.wsgi_soap import SimpleWSGISoapApp from soaplib.wsgi_soap import SimpleWSGISoapApp from soaplib.service import soapmethod from soaplib.serializers.primitive import * from cherrypy.wsgiserver import CherryPyWSGIServer class ServersInfoService(SimpleWSGISoapApp): @soapmethod(_returns=Array(String)) def getServersList(self): results = [] for serverValues in Server.objects.values(): results.append(str(serverValues)) return results if __name__=='__main__': try: server = CherryPyWSGIServer(('localhost' ,17777), ServersInfoService()) # enable the next two lines if you want HTTPS #server.ssl_private_key = '/fullpath/file.key' #server.ssl_certificate = '/fullpath/file.pem' server.start() except KeyboardInterrupt: server.stop()
Client code sample:
from soaplib.client import make_service_client import unittest import os import re class infoServicesTests(unittest.TestCase): def setUp(self): # https=True enables HTTPS self.clientInfoService = make_service_client('localhost:17777/', ServersInfoService(), https=True) def testGetServiceWSDL(self): # this generates the WSDL: print self.clientInfoService.server.wsdl('') def testGetServersList(self): servers = self.clientInfoService.getServersList() for tuple in servers: print "Servidor:" + tuple
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.
Pingback: Python web services with SoapLib | Currucuxu()