python webservice

python webservice

    • 前言
    • 遇到的问题
    • 过程

前言

当项目比较大,数据量比较多的时候。根据业务拆分成多个系统就显得非常重要了。各个系统的交互肯定有数据交互,使用webservice就显得很重要了。比如我要把a系统的数据提供给b系统使用,就需要a系统封装一个api接口提供给b系统使用

遇到的问题

1、开始使用的是soaplib,在官网看到最新版的是2011年更新的。当时也没有放在心上,在安装使用过程中一直遇到很多问题,后面才知道soaplib只支持python2。后面才选用现在的spyne,spyne官网一直有更新,可支持python3.6
2、spyne结合使用django。启动成功后访问http://127.0.0.1:8000/出现404(如图),这是正常的。具体原因我也不知道
python webservice_第1张图片
3、千万不要在同一django项目里面使用suds 客户端,不然会调用不成功。

过程

1、安装 spyne,subs(命令 pip3 install spyne/subs)
2、服务端代码:
from django.shortcuts import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from spyne import Application, rpc, ServiceBase, Unicode, Integer
from spyne import Iterable
from spyne.protocol.soap import Soap11
from spyne.server.django import DjangoApplication

class HelloWorldService(ServiceBase):

@rpc(Integer, _returns=Iterable(Unicode))
def count_sum(ctx, sumNum):
    for i in range(0, 5):
        sumNum += i
        print(sumNum)
    return HttpResponse(sumNum)

application = Application([HelloWorldService],
tns=‘spyne.examples.hello’,
in_protocol=Soap11(validator=‘lxml’),
out_protocol=Soap11()
)

sum_app = csrf_exempt(DjangoApplication(application))

3、配置django urls.py文件如下:
from django.conf.urls import url

from djiangosix import views

urlpatterns = [
url(r’^sumall/’, views.sum_app),
]

4、客户端代码:
from suds.client import Client
import traceback

try:
client = Client(“http://127.0.0.1:8000/sumall/?wsdl”)
result = client.service.count_sum(0)
print(result)
except:
traceback.print_exc()
5、先启动服务端,再启动客户端
服务端结果:
[17/Jun/2019 10:08:44] “GET /sumall/?wsdl HTTP/1.1” 200 2719
0
1
3
6
10
客户端结果:
(stringArray){
string[] =
“10”,
}

你可能感兴趣的:(python,个人)