Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fake a soap response in Python?
    primarykey
    data
    text
    <p>I am trying to test a function in company product. Our software will make a SOAP request like this:</p> <p><strong>Request Header</strong></p> <pre><code>POST /testfunction.php HTTP/1.1 Accept: application/soap+xml, application/xml, text/xml SOAPAction: "http://www.abc.com/testfunction#test" Host: soap.abc.com Content-Length: 461 Connection: Keep-Alive </code></pre> <p><strong>Request Content</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.abc.com/testfunction"&gt; &lt;SOAP-ENV:Body&gt; &lt;ns1:test&gt; &lt;productID&gt;3&lt;/productID&gt; &lt;productSubID&gt;1&lt;/productSubID&gt; &lt;version&gt;1.0.1&lt;/version&gt; &lt;serialNumber/&gt; &lt;language&gt;EN&lt;/language&gt; &lt;daysLicensed&gt;0&lt;/daysLicensed&gt; &lt;daysLeft&gt;0&lt;/daysLeft&gt; &lt;/ns1:test&gt; &lt;/SOAP-ENV:Body&gt; &lt;/SOAP-ENV:Envelope&gt; </code></pre> <p>And the SOAP service should respond:</p> <p><strong>Response Header</strong></p> <pre><code>HTTP/1.1 200 OK Server: nginx/0.7.67 Date: Mon, 02 May 2011 13:43:46 GMT Content-Type: text/xml; charset=utf-8 Connection: keep-alive X-Powered-By: PHP/5.3.3-1ubuntu9.3 Content-Length: 304 Content-encoding: gzip </code></pre> <p><strong>Response Content</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.abc.com/testfunction"&gt;&lt;SOAP-ENV:Body&gt;&lt;ns1:testResponse&gt;&lt;result&gt;1000&lt;/result&gt;&lt;/ns1:testResponse&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt; </code></pre> <p>In the beginning I thought I could just make a web service in web.py and returns the same response whenever someone make a POST request on <a href="http://www.abc.com/testfunction" rel="nofollow">http://www.abc.com/testfunction</a>.</p> <pre><code>import web url = ( '/testfunction', 'testfunction', ) class testfunction: def POST(self): web.header('Content-Type', 'text/xml; charset=utf-8') return """&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.abc.com/testfunction"&gt;&lt;SOAP-ENV:Body&gt;&lt;ns1:testResponse&gt;&lt;result&gt;1000&lt;/result&gt;&lt;/ns1:testResponse&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt; """ app = web.application(url, globals()) if __name__== "__main__": app.run() </code></pre> <p>But it didn't work. I think that maybe something to do with the header. Then I tried with SimpleXMLRPCServer.</p> <pre><code>from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler import xmlrpclib class MyRequestHandler(SimpleXMLRPCRequestHandler): rpc_paths = ('/',) def do_POST(self): return SimpleXMLRPCRequestHandler.do_POST(self) def test(): return "hello, world" server = SimpleXMLRPCServer( ("0.0.0.0", 80), requestHandler = MyRequestHandler, ) server.register_function(test, 'test') server.serve_forever() </code></pre> <p>but problem is I don't know how to deal with SOAPAction in the header and the client is not using the test function here. Can anyone help me? Thanks a lot!!</p> <p><strong>Update:</strong></p> <p>Finally did it, using the following code:</p> <pre><code>from wsgiref.simple_server import WSGIServer, WSGIRequestHandler def echo(environ, start_response): status = "200 OK" headers = [("Content-type", "text/xml")] start_response(status, headers) return ["""&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.abc.com/testfunction"&gt;&lt;SOAP-ENV:Body&gt;&lt;ns1:testResponse&gt;&lt;result&gt;1000&lt;/result&gt;&lt;/ns1:testResponse&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;"""] httpd = WSGIServer(('0.0.0.0', 80), WSGIRequestHandler) httpd.set_app(echo) httpd.serve_forever() </code></pre> <p>It should work the same with the web.py code, however the web.py one doesn't. From the captured package in wireshark I found some garbled code ("3bc" and "0) before and after the xml content, something to do with the encoding?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload