如何在Python中伪造soap响应?

2024-09-28 05:27:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试公司产品中的一个功能。我们的软件将发出如下SOAP请求:

请求头

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

请求内容

^{pr2}$

并且SOAP服务应该响应:

响应标题

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

回复内容

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.abc.com/testfunction"><SOAP-ENV:Body><ns1:testResponse><result>1000</result></ns1:testResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

一开始我以为我可以在网页.py当有人对http://www.abc.com/testfunction发出POST请求时,返回相同的响应。在

import web                                                                                                                                                                                                                                                                                                                                                                                                                    
url = (                                                                                                                                                                                                                                                                                                                                                                                       
    '/testfunction', 'testfunction',                                                                                                                                                             
)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

class testfunction:                                                                                                                                                                                             
    def POST(self):                                                                                                                                                                                            
        web.header('Content-Type',  'text/xml; charset=utf-8')                                                                                                                                                 
        return """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.abc.com/testfunction"><SOAP-ENV:Body><ns1:testResponse><result>1000</result></ns1:testResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>               
                """                                                                                                                                                                                            

app = web.application(url, globals())                                                                                                                                                                          

if __name__== "__main__":                                                                                                                                                                                      
    app.run()  

但没用。我想这可能和头球有关。然后我尝试使用SimpleXMLRPCServer。在

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()

但问题是我不知道如何处理头中的SOAPAction,而且客户机没有使用这里的测试函数。有人能帮我吗?谢谢!!在

更新:

最后,使用以下代码:

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 ["""<?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.abc.com/testfunction"><SOAP-ENV:Body><ns1:testResponse><result>1000</result></ns1:testResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>"""]

httpd = WSGIServer(('0.0.0.0', 80), WSGIRequestHandler)
httpd.set_app(echo)
httpd.serve_forever()

它应该和网页.py但是网页.py从wireshark捕获的包中,我发现在xml内容前后有一些乱码(“3bc”和“0”),与编码有关吗?在


Tags: envcomhttpbodyxmlresultcontentpost
1条回答
网友
1楼 · 发布于 2024-09-28 05:27:27

您可以使用soaplib创建一个真正的SOAP服务,该服务实现您的接口并返回虚拟数据。与创建手写静态响应相比,这应该更容易维护,并且代码不应该比网页.py-以身作则。在

这是一个Hello World example。在

相关问题 更多 >

    热门问题