python django及脚本化开发webserve接口(spyne)

想用django写个webserver接口,网上各种搜,整理下本次的记录

看到很多使用souplib的包,我用的python3.7,导包一直有问题,然后看到网上有人说souplib只支持python2,并且在2011年后不再更新,最后换个包spyne,之前有个文章是写调用webserver的封装,此次就简单写下调用。

views.py

import logging

from django.views.decorators.csrf import csrf_exempt
from spyne import Application, rpc, ServiceBase, Unicode, Iterable, String, Integer
from spyne.model.complex import ComplexModel
from spyne.protocol.soap import Soap11
from spyne.server.django import DjangoApplication

# 日志配置
logging.basicConfig(level=logging.DEBUG, filename='my_server.log')
logging.getLogger('server').setLevel(logging.DEBUG)


# 声明服务类,类的方法就是客户端访问的服务,直接访问方法名就行
# Unicode:参数类型,可有有多个参数,有几个写几个
# _returns:响应类型
class MyServer(ServiceBase):

    # 返回字符类型
    @rpc(Unicode, Unicode,_returns=Unicode)
    def make_func(self, par1, par2):
        print(par1)
        #业务逻辑放这里
        return "success"

    # 返回数组类型
    @rpc(Unicode, _returns=Iterable(Unicode))
    def json_ret(self, params1):
        ary = ["test", "111", "ceshi", "222"]
        return ary
    
    # 传入字典类型的参数,此User可以只传部分参数,或者一个都不传也可以,不能传没有的key,会报错
    @rpc(User, _returns=Unicode)
    def make_func(self, project):
        print(User)
        return "success"


# 传入字典类型的参数,需要声明一个类
class User(ComplexModel):
    __namespace__ = 'User'
    name = Unicode
    phone = Unicode
    address = Unicode
    age = Integer


soap_app = Application([SServices],
                           'MyServer',
                           in_protocol=Soap11(validator="lxml"),
                           out_protocol=Soap11())
# DjangoApplication(django使用这个,还有其他好几几种,可以以脚本方式启动等等)
# 访问,urls.py中,映射server_app就可以了 
server_app = csrf_exempt(DjangoApplication(soap_app))

测试test.py

from suds.client import Client
cilent=Client('http://ip:port/urls中的路由?wsdl')
# client.service.方法名 (调用格式)
ret = client.service.make_func()

还可以不使用django开发,脚本开发启动,直接运行文件即可访问

# Application is the glue between one or more service definitions, interface and protocol choices.
from spyne import Application
# @rpc decorator exposes methods as remote procedure calls
# and declares the data types it accepts and returns
from spyne import rpc
# spyne.service.ServiceBase is the base class for all service definitions.
from spyne import ServiceBase
# The names of the needed types for implementing this service should be self-explanatory.
from spyne import Iterable, Integer, Unicode

from spyne.protocol.soap import Soap11
# Our server is going to use HTTP as transport, It’s going to wrap the Application instance.
from spyne.server.wsgi import WsgiApplication


# step1
class HelloWorldService(ServiceBase):
    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello(self, name, times):

        for i in range(times):
            yield u'Hello, %s' % name


# step2
soap_app = Application([HelloWorldService], 'hello',
                       in_protocol=Soap11(validator='lxml'),
                       out_protocol=Soap11())

# step3
wsgi_app = WsgiApplication(soap_app)

if __name__ == '__main__':
    import logging

    from wsgiref.simple_server import make_server

    # configure the python logger to show debugging output
    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)

    server = make_server('127.0.0.1', 8005, wsgi_app)
    server.serve_forever()

简单写到这儿,以备下次使用

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值