Skip to content

Instantly share code, notes, and snippets.

@plq
Created November 21, 2012 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plq/4124822 to your computer and use it in GitHub Desktop.
Save plq/4124822 to your computer and use it in GitHub Desktop.
[Soap-Python] Problem with simpleContent
Client's consol:
================================================================================
SENT:
<?xml version="1.0" encoding="UTF-8"?>
No handlers could be found for logger "suds.umx.typed"
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="spyne.test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:testMethod>
<ns1:req>
<ns1:Params>
<ns1:Param name="foo1">bar1</ns1:Param>
<ns1:Param name="foo2">bar2</ns1:Param>
</ns1:Params>
</ns1:req>
</ns1:testMethod>
</ns0:Body>
</SOAP-ENV:Envelope>
================================================================================
RECEIVED:
<?xml version="1.0" encoding="UTF-8"?>
<senv:Envelope>
<senv:Body>
<tns:testMethodResponse>
<tns:testMethodResult>
<tns:Params>
<tns:Param name="bar1">foo1</tns:Param>
<tns:Param>foo2</tns:Param>
</tns:Params>
</tns:testMethodResult>
</tns:testMethodResponse>
</senv:Body>
</senv:Envelope>
================================================================================
Server's consol:
<ns0:ReqMessage xmlns:ns0="spyne.test">
<ns0:Params>
<ns0:Param>bar1</ns0:Param>
<ns0:Param>bar2</ns0:Param>
</ns0:Params>
</ns0:ReqMessage>
# -*- coding: utf-8 -*-
__author__ = 'kulapard'
import logging
logging.basicConfig(level=logging.DEBUG)
from lxml import etree
from spyne.util.xml import get_object_as_xml
from spyne.application import Application
from spyne.decorator import srpc
from spyne.service import ServiceBase
from spyne.model.primitive import Unicode
from spyne.model.complex import ComplexModel
from spyne.model.complex import XmlAttribute
from spyne.protocol.soap import Soap11
from spyne.interface.wsdl import Wsdl11
APP_NAMESPACE = 'spyne.test'
class Params(ComplexModel):
__namespace__ = APP_NAMESPACE
Param = Unicode(min_occurs=0, max_occurs='unbounded')
name = XmlAttribute(Unicode, attribute_of='Param')
class ReqMessage(ComplexModel):
__namespace__ = APP_NAMESPACE
Params = Params
class ResMessage(ComplexModel):
__namespace__ = APP_NAMESPACE
Params = Params
class TestService(ServiceBase):
@srpc(ReqMessage, _returns=ResMessage)
def testMethod(req):
print etree.tostring(get_object_as_xml(req), pretty_print=True)
res = ResMessage()
res.Params = Params(Param=['foo1', 'foo2'], name='bar1')
return res
application = Application([TestService],
name='TestService',
tns=APP_NAMESPACE,
interface=Wsdl11(),
in_protocol=Soap11(),
out_protocol=Soap11(cleanup_namespaces=False)
)
if __name__ == '__main__':
# You can use any Wsgi server. Here, we chose
# Python's built-in wsgi server but you're not
# supposed to use it in production.
from wsgiref.simple_server import make_server
from wsgiref.validate import validator
from spyne.server.wsgi import WsgiApplication
wsgi_app = WsgiApplication(application)
server = make_server('127.0.0.1', 7788, validator(wsgi_app))
print "listening to http://127.0.0.1:7788"
print "wsdl is at: http://127.0.0.1:7788/?wsdl"
server.serve_forever()
# -*- coding: utf-8 -*-
__author__ = 'kulapard'
from suds.client import Client
from suds.client import WebFault
from suds.plugin import MessagePlugin
URL = 'http://127.0.0.1:7788/?wsdl'
class AddAttributToParam(MessagePlugin):
def marshalled(self, context):
body = context.envelope.getChild('Body')
params = body[0][0][0]
params[0].set('name', 'foo1')
params[1].set('name', 'foo2')
def main():
client = Client(URL, cache=None, plugins=[AddAttributToParam()])
req = client.factory.create('ReqMessage')
req.Params.Param = ['bar1', 'bar2']
try:
res = client.service.testMethod(req)
except WebFault, e:
print e.fault
print '='*80
print 'SENT:'
print client.last_sent()
print '='*80
print 'RECEIVED:'
print client.last_received()
print '='*80
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment