用soaplib的django webserver

前面寫過怎麼利用suds來調用webservicePython調用基於https協議的SOAP WebService,這篇講的是如何用soaplib開發SOAP WebService(最近發現國外開源社區裏把json方式的別的Web服務也叫作WebService,叫法跟Java和.Net的約定叫法不太同樣,這裏加上SOAP以跟json格式的WebService區分開來)。python

第一步,固然是安裝問題了:web

下載soaplib:django

這是下載地址,我選了soaplib 2.0.0-beta2下載,由於接下來要用到一個djangosnippet是基於2.0的。json

在開發環境(Mac OS X 10.7.4)上安裝很順利:centos

 $ python setup.py install


但在測試環境(CentOS)上卻碰到了一點麻煩,執行上述安裝時報錯:error: Setup script exited with error: command 'gcc' failed with exit status 1。網上找到有人提出的解決方案是安裝libxml2, libxml2-devel, libxslt-devel,使用yum安裝之。app

 $yum install libxml2 $yum install libxml2-devel $yum install libxslt-devel

這裏有個小技巧,通常查到debian下安裝這類包時使用apt-get,而CentOS使用yum,當看到以apt-get的方式安裝的時叫apt-get install libxml2-dev之類的,只要把把dev換成devel,就可使用yum在centos或者redhat下安裝了,可是此次我上了個當,網上查到好幾個地方的libxslt安裝都是apt-get install libxslt1-dev,可是yum包沒有那個「1」。。。後來我去下載lxml2.3.4源碼包,看到下面那一段話測試

 

* On **Linux** (and most other well-behaved operating systems), ``easy_install`` will manage to build the source distribution as long as libxml2 and libxslt are properly installed, including development packages, i.e. header files, etc. Use your package management tool to look for packages like ``libxml2-dev`` or ``libxslt-devel`` if the build fails, and make sure they are installed.網站

纔去掉那個「1」,安裝成功。ui

接下來去下載一個叫SOAP web service with soaplib 2.0的東東,由於最近剛過兒童節,風緊,聽說好多org域名的境外網站貌似都不問青紅皁白被牆了,把代碼貼出以下:url

from soaplib.core import Application
from soaplib.core.server.wsgi import Application as WSGIApplication
from django.http import HttpResponse


class DjangoSoapApp(WSGIApplication):
    """
    Generic Django view for creating SOAP web services (works with soaplib 2.0)

    Based on http://djangosnippets.org/snippets/2210/
    """

    csrf_exempt = True

    def __init__(self, services, tns):
        """Create Django view for given SOAP soaplib services and tns"""

        return super(DjangoSoapApp, self).__init__(Application(services, tns))

    def __call__(self, request):
        django_response = HttpResponse()

        def start_response(status, headers):
            django_response.status_code = int(status.split(' ', 1)[0])
            for header, value in headers:
                django_response[header] = value

        response = super(DjangoSoapApp, self).__call__(request.META, start_response)
        django_response.content = '\n'.join(response)

        return django_response

djangosnippets.org是個好網站,有不少頗有用的django小部件,缺點是,容易莫名其妙被牆,以及Usage太簡單,很難偷懶不閱讀他的源碼直接使用,這個snippet的Usage就只有一行字:

Usage is the same as before: my_soap_service = DjangoSoapApp([MySOAPService], __name__)

它以前的版本也很是簡單,並且那些引用在這個版本里都已經移動位置了,不得已,只得去閱讀soaplib2.0的源碼,把相應的位置糾正過來。

我來寫個比他詳細點的Usage吧:

View.py裏的代碼:

#這幾個引用,soablib2.0的位置跟0.9+之類的版本不同了
from soaplib.core.model.primitive import Boolean, String
from soaplib.core.service import DefinitionBase, rpc

# the class with actual web methods
class MySOAPService(DefinitionBase):
    @rpc(String, String, _returns=Boolean)
    def Test(self, f1, f2):
        return True
    @rpc(String, _returns=String)
    def HelloWorld(self, name):
        return 'Hello %s!' %name


my_soap_service = DjangoSoapApp([MySOAPService], 'laonan.net')

 

urls.py裏的代碼:

 url(r'^my-soap-service/service', 'yourproject.yourapp.views.my_soap_service'),
 url(r'^my-soap-service/service.wsdl', 'yourproject.yourapp.views.my_soap_service'),

上面這兩個地址必定要配對,最好到wsdl文檔裏去確認下soap:address節點的location屬性,最先我配的時候,第一個url少配了service那一段,還覺得是soaplib2是beta版有bug,怎麼老找不到,查了半天才發現是這裏配錯了。

下面來測試下:

1)安裝suds

安裝python

下載setuptools-0.6c9.tar.gz

#python setup.py install

下載python-suds-0.4.tar.gz

#python setup.py install

 

進行python命令行

>>> from suds.client import Client

>>> hello_client = Client('http://127.0.0.1:8080/my-soap-service/service/?wsdl')

>>> result = hello_client.service.HelloWorld("carlos")

>>> print result

Hello carlos

相關文章
相關標籤/搜索