soap_examples.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. # SOAP webservices (server and client) example and basic test
  3. # (using pysimplesoap contrib included in web2py)
  4. # for more info see: https://code.google.com/p/pysimplesoap/wiki/Web2Py
  5. from gluon.tools import Service
  6. service = Service(globals())
  7. # define the procedures to be exposed:
  8. @service.soap('AddStrings', returns={'AddResult': str}, args={'a': str, 'b': str})
  9. @service.soap('AddIntegers', returns={'AddResult': int}, args={'a': int, 'b': int})
  10. def add(a, b):
  11. "Add two values"
  12. return a+b
  13. @service.soap('SubIntegers', returns={'SubResult': int}, args={'a': int, 'b': int})
  14. def sub(a, b):
  15. "Substract two values"
  16. return a-b
  17. @service.soap('Division', returns={'divisionResult': float}, args={'a': float, 'b': float})
  18. def division(a, b):
  19. "Divide two values "
  20. return a / b
  21. # expose the soap methods
  22. def call():
  23. return service()
  24. # sample function to test the SOAP RPC
  25. def test_soap_sub():
  26. from gluon.contrib.pysimplesoap.client import SoapClient, SoapFault
  27. # build the url to the WSDL (web service description)
  28. # like "http://localhost:8000/webservices/sample/call/soap?WSDL"
  29. url = URL(f="call/soap", vars={"WSDL": ""}, scheme=True)
  30. # create a SOAP client
  31. client = SoapClient(wsdl=url)
  32. # call the SOAP remote method
  33. try:
  34. ret = client.SubIntegers(a=3, b=2)
  35. result = ret['SubResult']
  36. except SoapFault as sf:
  37. result = sf
  38. response.view = "soap_examples/generic.html"
  39. return dict(xml_request=client.xml_request,
  40. xml_response=client.xml_response,
  41. result=result)