python中使用twisted和cyclone的简单后端rest框架

pypolyback的Python项目详细描述


欢迎回来!一个简单的后端restful框架!

  • twisted-web事件处理 框架(低层)
  • cyclone-http框架(高 水平)

安装

  1. 安装python 2.7
    • 窗口-Link
    • ubuntu-sudo apt-get install python2
    • 软呢帽-sudo yum install python2
    • 拱门-sudo pacman -S python2
  2. 安装pip-python库管理器
    • 窗口- Link
    • ubuntu-sudo apt-get install pip2
    • 软呢帽-sudo yum install pip2
    • 拱门-sudo pacman -S pip2
  3. 使用pip
      安装此框架
    • pip2 install pypolyback

初始化项目

$ 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

reqcyclone的请求,包含以下属性:* 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})

现在按照说明进行测试,并查看端点是如何工作的


启动服务器

有两种方法可以启动服务器

  1. 在根项目上从终端执行pypolyback --start。 文件夹(推荐)
  2. 从模块pypolyback.server(仅)调用方法start()。 如果在启动服务器之前需要执行某些操作,建议您执行此操作)

观察

框架和本页都在开发中,因此 变化。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
从Java中的方法返回列表时遇到问题   java如何忽略json字段,并使用Jackson ObjectMapper获取其值以进行映射   spring通过更新其各自java对象的值来映射两个xsd文件   java从HttpClient获取500错误,在浏览器中工作   java使用物理键输入(耳机中的按钮)在安卓中执行一些操作   如何在int数组(java)中追加int?   java Spring RequestParam的默认值等于方法调用   java将JsonLayout添加到log4j2 json配置   Ubuntu上的maven Tomcat6 libs和/usr/share/java   java单元测试Android活动   java获取URL证书的屏幕截图   java如何为自定义类加载器加载的类提供工具?   FB墙上的java错误图片,来自安卓应用程序的反馈帖子(安卓 FB sdk)   从Intellij IDEA内部运行Tomcat时的java差异?   java TDD与不可能的例外   安卓 Java日期表示差异