2011-05-03 33 views
3

我想在公司產品中測試一個函數。我們的軟件將讓這樣的SOAP請求:如何在Python中僞造肥皂響應?

請求頭

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 

請求內容

<?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:test> 
<productID>3</productID> 
<productSubID>1</productSubID> 
<version>1.0.1</version> 
<serialNumber/> 
<language>EN</language> 
<daysLicensed>0</daysLicensed> 
<daysLeft>0</daysLeft> 
</ns1:test> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

和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> 

在我想我可能只是使web.py一個Web服務,並返回每當有人就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() 

它應該與web.py代碼相同,但web.py一個沒有。從Wireshark的捕獲的包,我發現了一些亂碼(「3BC」和「0)之前和XML內容之後,是與編碼?

回答

7

你可以使用soaplib創建它實現了真正的SOAP服務您的界面並返回虛擬數據。這應該是有點更容易維護然後創建手寫的靜態響應,並且代碼不應該比你的基於web.py,例如更長的時間。

這裏有一個Hello World example

+0

謝謝爲你答案!即使通過我最終沒有使用你的解決方案,但soaplib文檔仍然值得一讀:) – 2011-05-06 14:23:40