python中使用twisted和cyclone的简单后端rest框架
pypolyback的Python项目详细描述
欢迎回来!一个简单的后端restful框架!
安装
初始化项目
$ pypolyback --init
使用
目录
/pypolyback (*) - api internal files /config - json configuration files /endpoints - backend endpoints /utils - helper script files Note (\*): The dir pypolyback will only exists if you didn't install ``pypolyback``'s library from pip
api
下面是在端点后面工作的所有api文件
配置
以下是应用程序中使用的配置文件。他们会被送到 端点通过paramapi.config
有3个特殊的文件名:*prod.json-官方 配置文件*dev.json-开发配置文件 *local.json-本地配置文件(在git中忽略)
Note: They really work as following: the api tries to load ^{tt11}$, then ^{tt10}$, then ^{tt9}$. So in the oficial release you will only have ^{tt9}$
当前的配置特殊属性如下:
{"log":bool,//optional.default=False"server":{"port":int,//optional.default=8888"cors":stringorFalse//optional.default=False},"mail":{"host":string,"port":int,//optional.default=25or587forTLS"tls":bool,//optional.default=False"username?":string,//optional.nodefault"password?":string//optional.nodefault}}
终点
这将是您的主要开发目录
此处添加的所有文件将自动成为终结点
即:文件endpoints/test/helloworld.py将生成 端点/test/helloworld
文件的代码如下:
utils=['[util1]','[util2]'][@async]def[method](req,api):[process]
其中[method]是http请求类型:*post*get*put* delete*head*options*default-请求时执行 对于以上任何一项,但它没有实现
[process]是您希望端点执行的操作(您的代码)
[util1]和[util2]是utils脚本(不带.py)
req是cyclone的请求,包含以下属性:* params-从请求、对象(原语、列表或 dictionary)*send-用对象响应请求的函数
^{tt23}$’s complete documentatios in present on cyclone’s site http://cyclone.io/documentation/web.html
api是包含所有api功能的对象:*config- 在实际作用域*中使用的配置字典调试-函数 记录消息*错误-记录错误的函数
[@async]是一个可选的注释,它使此方法 异步。
注意:如果使用async,则需要导入它 (from pypolyback import async)
^{tt28}$ complete doc is the same as twisted’s ^{tt29}$ https://twistedmatrix.com/documents/current/api/twisted.internet.defer.html#inlineCallbacks
utils
具有可重用代码的python文件,将在端点上调用。
这将是一个正常的cod,但有一些特殊功能:
初始化(API)
The function that will be executed on server startup Only one time.
[method](req,api)-[method]是http请求的类型
The function that will be called before every request to the function with the same name on the endpoint. Any result should be stored on the variable `req`, because it is the only local variable on the request.
任何(要求,API)
The function that will be executed before any request. Note: thids function will be executed before the later.
应用程序py
This file is not needed if you installed from pip
启动服务器的可执行文件
示例
要了解事情的进展情况,请看一下文件 endpoints/example/ex_endpoint.py
应该是这样的:
#!/usr/bin/env python# -*- coding: utf-8 -*-frompypolybackimportasyncutils=['example_util']@async#método asíncronodefget(req,api):""" Start the server Then go, from your browser, in `localhost:8888/example/ex_endpoint` There shoud open a page with the content `Success in method get!` Output: string """result=yieldapi.example_util.write(req)#coletando dados de forma asíncronareq.send(result)#retornando os dadosdefpost(req,api):""" Start the server Then make a post http request to `localhost:8888/example/ex_endpoint` Sending the documented object as input It should be returned `{"message": input.message, "status":"Sucess in method post!"}` Input: message: string Output: message: string request: string """message=req.params['message']#coletando dados da requisição#retornando os dadosreq.send({'message':api.example_util.write(req),'request':message})
现在按照说明进行测试,并查看端点是如何工作的
启动服务器
有两种方法可以启动服务器
- 在根项目上从终端执行pypolyback --start。 文件夹(推荐)
- 从模块pypolyback.server(仅)调用方法start()。 如果在启动服务器之前需要执行某些操作,建议您执行此操作)
观察
框架和本页都在开发中,因此 变化。