Skip to content

Instantly share code, notes, and snippets.

@plq
Created May 17, 2012 12:58
Show Gist options
  • Save plq/2718747 to your computer and use it in GitHub Desktop.
Save plq/2718747 to your computer and use it in GitHub Desktop.
Using rpclib with wsdl containing namespaced messages
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:calc="http://www.example.com/wsdl/calc/v1.0"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/wsdl/calc/v1.0"
name="CalcWebService">
<wsdl:types>
<xsd:schema
targetNamespace="http://www.example.com/wsdl/calc/v1.0"
xmlns:calc="http://www.example.com/wsdl/calc/v1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:simpleType name="FuncType">
<xsd:restriction base="xsd:NMTOKEN">
<xsd:enumeration value="ADD"/>
<xsd:enumeration value="SUB"/>
<xsd:enumeration value="MUL"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="calcRequestParameters">
<xsd:sequence>
<xsd:element name="calcFuncId" type="calc:FuncType" minOccurs="1" maxOccurs="1"/>
<xsd:element name="calcRequest" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="calcRequest" type="calc:calcRequestParameters"/>
<xsd:element name="calcResponse" type="xsd:string"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="calcResult">
<wsdl:part name="response" element="calc:calcResponse"/>
</wsdl:message>
<wsdl:message name="calcMessage">
<wsdl:part name="request" element="calc:calcRequest"/>
</wsdl:message>
<wsdl:portType name="calcMessagePortType">
<wsdl:operation name="SendCalcMessage">
<wsdl:input message="calc:calcMessage"/>
<wsdl:output message="calc:calcResult"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalcService" type="calc:calcMessagePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="SendCalcMessage">
<wsdlsoap:operation soapAction="http://calc.example.com/SendCalcMessage" style="document"/>
<wsdl:input>
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CalcAPIService">
<wsdl:port name="CalcAPI" binding="calc:CalcService">
<wsdlsoap:address location="http://www.example.com/soap-service/CalcService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
import os
from os import path
from suds.client import Client
winslash = '' if os.getcwd().startswith('/') else '/'
wsdl_uri = 'file://' + winslash + path.join(os.getcwd(), 'badcalc.wsdl')
client = Client(wsdl_uri, location='http://localhost:8082')
print 'Result : ', client.service.SendCalcMessage('ADD', '1 2')
'''
ERROR: I get following exception
Traceback (most recent call last):
File "calcclient.py", line 9, in <module>
print 'Result : ', client.service.SendCalcMessage('ADD', '1 2')
File "C:\Python27\lib\site-packages\suds\client.py", line 542, in __call__
return client.invoke(args, kwargs)
File "C:\Python27\lib\site-packages\suds\client.py", line 602, in invoke
result = self.send(soapenv)
File "C:\Python27\lib\site-packages\suds\client.py", line 649, in send
result = self.failed(binding, e)
File "C:\Python27\lib\site-packages\suds\client.py", line 702, in failed
r, p = binding.get_fault(reply)
File "C:\Python27\lib\site-packages\suds\bindings\binding.py", line 265, in get_fault
raise WebFault(p, faultroot)
suds.WebFault: Server raised fault: 'Method '{http://www.example.com/wsdl/calc/v1.0}calcRequest' not found.'
'''
from rpclib.decorator import srpc
from rpclib.service import ServiceBase
from rpclib.model.binary import ByteArray
from rpclib.model.enum import Enum
from rpclib.model.primitive import String, Integer
from rpclib.model.complex import Array, ComplexModel
CALC_WSDL_NS = 'http://www.example.com/wsdl/calc/v1.0'
FuncType = Enum('ADD', 'SUB', 'MUL', type_name="FuncType")
FuncType.__namespace__ = CALC_WSDL_NS
class CalcService(ServiceBase):
@srpc(FuncType, String, _returns=String, _in_message_name="calcRequest", _body_style='bare', _soap_body_style='document')
def SendCalcMessage(calcFuncId, calcRequest):
num1, num2 = calcRequest.split()
if calcFuncId is FuncType.ADD:
return unicode(int(num1) + int(num2))
elif calcFuncId == 'SUB':
return unicode(int(num1) - int(num2))
elif calcFuncId == 'MUL':
return unicode(int(num1) * int(num2))
if __name__ == '__main__':
import logging
from wsgiref.simple_server import make_server
from rpclib.util.simple import wsgi_soap11_application
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('rpclib.protocol.xml').setLevel(logging.DEBUG)
wsgi_app = wsgi_soap11_application([CalcService], CALC_WSDL_NS)
server = make_server('127.0.0.1', 8082, wsgi_app)
print "WSDL is at: http://localhost:8082/?wsdl"
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment