用ladon框架封装Python为Webservice接口以及调用接口的方法

简介: 一、用ladon框架封装Python为Webservice接口                 功能实现的同时,希望将接口开放给别人,而封装python接口的一个再简单不过的框架Ladon,而且提供不同的协议,包括SOAP和Json等格式的接口。

一、用ladon框架封装Python为Webservice接口             

   功能实现的同时,希望将接口开放给别人,而封装python接口的一个再简单不过的框架Ladon,而且提供不同的协议,包括SOAP和Json等格式的接口。本篇紧接着上上篇(Django部署)的。虽然很简单,但是官网的文档还是不够详细,下面介绍我配置的过程及遇到的问题。

1、安装Ladon包

使用Ladon框架,首先需要安装Ladon包(Ladon for Python),最新的是Ladon-0.8.9

2、新建一个APP(接着上个项目的话,就取名叫appapi)

1)、在这个app下面新建一个文件handler.py(实际上就是一个views):

# Create your views here.
from ladon.server.wsgi import LadonWSGIApplication
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'appops.settings'   #这里是项目的名称设置

application = LadonWSGIApplication(
  ['appapi.views'],  #引用当前目录下views里的内容
  [os.path.join(os.path.dirname(__file__), os.path.pardir)],
  catalog_name='OPS APP API',
  catalog_desc='This is the root of my cool webservice catalog')

在Windows环境下,其实这时会报错,报各种No modules named XXX的错误(上述from ladon.server.wsgi import LadonWSGIApplication 这句话需要三个包),这个好办,缺什么包装什么包,有个这个巨无霸Windows-Python集成包网站,啥都不缺,Ctrl+F就行!注意版本,这里很全,目前Windows下所有python的包都能在这里找到,32位和64位的都有,配置过程中,我就缺了下面三个包:

Jinja2-2.7.2.win32-py2.7.exe

MarkupSafe-0.18.win32-py2.7.exe

docutils-0.11.win32-py2.7.exe

2)、写接口函数(就是APP下的Views.py里的内容),被上面引用的['appapi.views']

以一个加法和减法为例:

from ladon.ladonizer import ladonize
from ladon.types.ladontype import LadonType

class Calculator(object):

    @ladonize(int,int,rtype=int)
    def add(self,a,b):
        return a+b

    @ladonize(int,int,rtype=int)
    def Minus(self,a,b):
        return a-b

class MyService(object):
    
    @ladonize(int,rtype=int)
    def mytest(self,a):
         return a*a

这里要注意两点:

a、要封装的方法必须写在一个类里(如这里的Class XXX)

b、类下面的方法如果需要封装,则方法前面必须加修饰符@ladonize,后面的参数分别是输入参数的类型和输出参数(return type)的类型

3、编辑配置文件

可以在apache_django_wsgi.conf文件里配置也可以在apache下的httpd.conf文件下配置,加上这句:

WSGIScriptAlias /api "D:/OPSAPP/appops/appapi/handler.py"

意思就是可以通过在主域名(或IP)后面加上/api/就可以访问到接口,而不用在urls.py里的urlpattern里写url了。

这里需要注意:

a、这句与WSGIScriptAlias / "D:/OPSAPP/django.wsgi"的顺序,上述api的别名一定要放在django.wsgi引用的前面,否则导致/api/的页面不能访问(试了很多次,WSGIScriptAlias /api "D:/OPSAPP/appops/appapi/handler.py"这个配置在apache_django_wsgi.conf里有问题,会出现主页和api页面不能同时访问的情况。但是有的同事就可以,不知道哪里配置不一样。我是配置在httpd.conf里的

b、如果不能访问api页面,错误日志会记录在apache目录下的logs/error.log里。配置过程中遇到了这个错误:assert sys.modules[modname] is old_mod 

这个需要改动源码:C:\Python27\Lib\site-packages\win32\lib\pywintypes.py第113行那段改动如下:

 if sys.version_info < (3,0):
        # assert sys.modules[modname] is old_mod
        # assert mod is old_mod
        pass
    else:
        assert sys.modules[modname] is not old_mod
        assert sys.modules[modname] is mod
        # as above - re-reset to the *old* module object then update globs.
        sys.modules[modname] = old_mod
        globs.update(mod.__dict__)

注意:每当修改过配置文件或者代码时,最好重启下apache服务,不然可能页面看不出改动后的效果。

至此没问题了,访问URL/api的界面如下:

点击Calculator接口,他提供了不同格式的接口类型:

我常用的就是第一个soap11类型的接口,点进去如下(不明觉厉,有木有): 

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:ns1="urn:Calculator" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:Calculator" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Calculator" targetNamespace="urn:Calculator">
<wsdl:types>
<xsd:schema xmlns:ns1="urn:Calculator" targetNamespace="urn:Calculator">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="add">
<wsdl:part name="a" type="xsd:long"/>
<wsdl:part name="b" type="xsd:long"/>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="result" type="xsd:long"/>
</wsdl:message>
<wsdl:message name="minus">
<wsdl:part name="a" type="xsd:long"/>
<wsdl:part name="b" type="xsd:long"/>
</wsdl:message>
<wsdl:message name="minusResponse">
<wsdl:part name="result" type="xsd:long"/>
</wsdl:message>
<wsdl:portType name="CalculatorPortType">
<wsdl:operation name="add">
<wsdl:input message="tns:add"/>
<wsdl:output message="tns:addResponse"/>
</wsdl:operation>
<wsdl:operation name="minus">
<wsdl:input message="tns:minus"/>
<wsdl:output message="tns:minusResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Calculator" type="tns:CalculatorPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add">
<soap:operation soapAction="http://192.168.18.74/api/Calculator/soap11/add" style="rpc"/>
<wsdl:input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Calculator" use="encoded"/>
</wsdl:input>
<wsdl:output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Calculator" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="minus">
<soap:operation soapAction="http://192.168.18.74/api/Calculator/soap11/minus" style="rpc"/>
<wsdl:input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Calculator" use="encoded"/>
</wsdl:input>
<wsdl:output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Calculator" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Calculator">
<wsdl:documentation>Ladon generated service definition</wsdl:documentation>
<wsdl:port binding="tns:Calculator" name="Calculator">
<soap:address location="http://192.168.18.74/api/Calculator/soap11"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
View Code

  

二、接口调用方法                                   

1、调用Soap11的接口

需要安装soap 的Client端包,大名叫做Suds(我的版本是suds-0.4),调用很简单,如下一个示例,不解释,聪明的你一看就明白:

#encoding:utf-8
import json
# import urllib,urllib2
from suds.client import Client

# import logging
# logging.basicConfig(level=logging.INFO)
# logging.getLogger('suds.client').setLevel(logging.DEBUG)

# username = 'api_user'
# password = 'api_pwd'
url="http://192.168.18.74/api/Calculator/soap11/description" #接口的URL headers = {'Content-Type': 'application/soap+xml; charset="UTF-8"'} client = Client(url,headers=headers,faults=False,timeout=15) def call_api_test(num1,num2): try: #--WSDL请求Header---------------------------------------------- # auth = client.factory.create('AuthenticationInfo') # auth.userName = username # auth.password = password # client.set_options(soapheaders=auth) #--WSDL请求Body---------------------------------------------- result = client.service.add(a=num1,b=num2) #print result if result[0] == 200: return (True, result[1]) else: return (False, result[1]) except Exception as e: return (False, e) if __name__ == '__main__': #get_software a= call_api_test(2,3) print (json.loads(a[1]))

 

2、调用Json格式的接口

……

 

 

 

 

作者: zhoujie
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,不然我担心博客园找你算账
如果您觉得本文对你有帮助,请竖起您的大拇指右下角点推荐,也可以关注我
目录
相关文章
|
12天前
|
存储 缓存 JavaScript
python实战篇:利用request库打造自己的翻译接口
python实战篇:利用request库打造自己的翻译接口
26 1
python实战篇:利用request库打造自己的翻译接口
|
10天前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【4月更文挑战第9天】本文对比了Python三大Web框架Django、Flask和Pyramid。Django功能全面,适合快速开发,但学习曲线较陡;Flask轻量灵活,易于入门,但默认配置简单,需自行添加功能;Pyramid兼顾灵活性和可扩展性,适合不同规模项目,但社区及资源相对较少。选择框架应考虑项目需求和开发者偏好。
|
29天前
|
数据采集 数据挖掘 调度
异步爬虫实践攻略:利用Python Aiohttp框架实现高效数据抓取
本文介绍了如何使用Python的Aiohttp框架构建异步爬虫,以提升数据抓取效率。异步爬虫利用异步IO和协程技术,在等待响应时执行其他任务,提高效率。Aiohttp是一个高效的异步HTTP客户端/服务器框架,适合构建此类爬虫。文中还展示了如何通过代理访问HTTPS网页的示例代码,并以爬取微信公众号文章为例,说明了实际应用中的步骤。
|
21小时前
|
存储 关系型数据库 MySQL
Python搭建代理IP池实现存储IP的方法
Python搭建代理IP池实现存储IP的方法
|
1天前
|
数据采集 存储 安全
python检测代理ip是否可用的方法
python检测代理ip是否可用的方法
|
3天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
25 0
|
4天前
|
前端开发 数据挖掘 API
使用Python中的Flask框架进行Web应用开发
【4月更文挑战第15天】在Python的Web开发领域,Flask是一个备受欢迎的轻量级Web框架。它简洁、灵活且易于扩展,使得开发者能够快速地构建出高质量的Web应用。本文将深入探讨Flask框架的核心特性、使用方法以及在实际开发中的应用。
|
5天前
|
关系型数据库 数据库 开发者
Python中的Peewee框架:轻量级ORM的优雅之旅
【4月更文挑战第13天】在Python的众多ORM框架中,Peewee以其轻量级、简洁和易于上手的特点,受到了许多开发者的青睐。Peewee的设计理念是“小而美”,它提供了基本的ORM功能,同时保持了代码的清晰和高效。本文将深入探讨Peewee的核心概念、使用场景以及实战应用,帮助读者更好地理解和使用这一框架。
|
5天前
|
SQL API 数据库
Python中的SQLAlchemy框架:深度解析与实战应用
【4月更文挑战第13天】在Python的众多ORM(对象关系映射)框架中,SQLAlchemy以其功能强大、灵活性和易扩展性脱颖而出,成为许多开发者首选的数据库操作工具。本文将深入探讨SQLAlchemy的核心概念、功能特点以及实战应用,帮助读者更好地理解和使用这一框架。
|
8天前
|
机器学习/深度学习 人工智能 算法

热门文章

最新文章