4

会社の製品で機能をテストしようとしています。私たちのソフトウェアは次のような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

コンテンツをリクエストする

<?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でキャプチャされたパッケージから、xmlコンテンツの前後に文字化けしたコード(「3bc」と「0」)が見つかりました。これはエンコーディングと関係がありますか?

4

1 に答える 1

8

soaplibを使用して、インターフェイスを実装し、ダミー データを返す実際の SOAP サービスを作成できます。これは、手書きの静的応答を作成するよりも維持するのが少し簡単で、コードは web.py ベースの例よりも長くないはずです。

これがHello World の例です。

于 2011-05-03T14:35:47.567 に答える