一、用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 importLadonWSGIApplicationimportos
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 importladonizefrom ladon.types.ladontype importLadonTypeclassCalculator(object):
@ladonize(int,int,rtype=int)defadd(self,a,b):return a+b
@ladonize(int,int,rtype=int)defMinus(self,a,b):return a-bclassMyService(object):
@ladonize(int,rtype=int)defmytest(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 notold_modassert sys.modules[modname] ismod#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.
Ladon generated service definition

View Code

二、接口调用方法

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

#encoding:utf-8
importjson# import urllib,urllib2from suds.client importClient#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" #接口的URLheaders= {'Content-Type': 'application/soap+xml; charset="UTF-8"'}
client= Client(url,headers=headers,faults=False,timeout=15)defcall_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])exceptException as e:return(False, e)if __name__ == '__main__': #get_software
a= call_api_test(2,3)
print (json.loads(a[1]))

2、调用Json格式的接口(困,睡觉去~)