SOaplibがWebserviceを実現

2551 ワード

1、setuptoolsをインストールし、以下のコマンドを実行する
 sudo apt-get install python-setuptools
このソフトウェアをインストールするのは、次のeasy_を使用するためです.インストールコマンドsoaplibのインストール
2、soaplibをインストールする
soaplibはTwisted、lxmlに依存し、lxmlはlibxmlおよびlibxsltに依存する
2.1 libxmlのインストール
libxml 2-2.7をダウンロードします.2.tar.gz、cdを解凍ディレクトリに解凍し、次のコマンドを実行します.
./configure
make
make install
2.2 libxsltをインストールし、オンラインでインストールし、次のコマンドを実行します.
sudo apt-get install libxslt-dev
2.3 pytzをインストールし、pytz-2012 hをダウンロードする.tar.bz、解凍、解凍ディレクトリに入り、次のコマンドを実行します.
sudo python setup install
2.4 soaplibをインストールし、次のコマンドを実行します.
easy_install soaplib
2.5クライアントはsudsをインストールする必要があり、https://pypi.python.org/pypi/sudsダウンロードtar.gz,解凍,解凍経路に入り,以下のコマンドを実行する
sudo python setup install
3、サービス側コードを作成する.
import soaplib
from soaplib.core.service import rpc, DefinitionBase
from soaplib.core.model.primitive import String, Integer
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import Array
from soaplib.core.service import soap

class HelloWorldService(DefinitionBase):
    @soap(String,Integer,_returns=Array(String))
    def say_hello(self,name,times):
        results = []
        for i in range(0,times):
            results.append('Hello, %s'%name)
        return results

if __name__=='__main__':
    try:
        from wsgiref.simple_server import make_server
        soap_application = soaplib.core.Application([HelloWorldService], 'tns')
        wsgi_application = wsgi.Application(soap_application)
        server = make_server('localhost', 7789, wsgi_application)
        server.serve_forever()
    except ImportError:
        print "Error: example server code requires Python >= 2.5"

以上のプログラムを起動し、
ブラウザでの再アクセスhttp://127.0.0.1:7789/SOAP/?wsdlのように
正常に動作すると、各メソッドの入力パラメータ、戻り値、およびエンティティクラスの情報を含むサービスの記述情報が表示されます.
4、クライアントプログラムの作成:
from suds.client import Client
hello_client = Client('http://localhost:7789/?wsdl')
result = hello_client.service.say_hello("Dave", 5)
print result

実行結果は次のとおりです.
string[] =        "Hello, Dave",       "Hello, Dave",       "Hello, Dave",       "Hello, Dave",       "Hello, Dave",
参照メッセージ:
http://blog.csdn.net/kerwin_liu/article/details/5777737
https://pypi.python.org/pypi/suds
http://www.cnblogs.com/grok/archive/2012/04/29/2476177.html
http://soaplib.github.com/soaplib/2_0/pages/helloworld.html