Pecan Introduce

  Pecan是一个轻量级的基于Python的Web框架,

  Pecan的目标并不是要成为一个“full stack”的框架,

  因此Pecan本身不支持类似Session和Databases.

Pecan Features

  (1) Object-dispatch for easy routing

  (2) Full support for REST-style controllers

  除了上述功能外,还包括以下扩展功能:

  Extensible security framework

  Extensible template language support

  Extensible JSON support

  Easy Python-based configuration

Pecan Web Application Files Structure

├── MANIFEST.in
├── config.py
├── public
│ ├── css
│ │ └── style.css
│ └── images
├── setup.cfg
├── setup.py
└── test_project
├── __init__.py
├── app.py
├── controllers
│ ├── __init__.py
│ └── root.py
├── model
│ └── __init__.py
├── templates
│ ├── error.html
│ ├── index.html
│ └── layout.html
└── tests
├── __init__.py
├── config.py
├── test_functional.py
└── test_units.py

  以上是一个默认的Pecan Web应用文件包结构。

  (1) config.py是整个应用的配置入口.

  (2) public文件夹存放Web应用所需的Image,CSS或者Javascript.

  (3) setup.py和setup.cfg用于Web应用的安装部署.

  (4) controllers存放路由控制文件.

  (5) templates存储Html或者Json的模板文件.

  (6) tests存放测试用例.

  PS:

    在测试环境可以使用pecan serve config.py启动Web应用.

    在商用环境下可以部署Pecan的Web应用到Apache等Web服务器.

Object-dispatch for easy routing

  以下是一个简单是Web应用示例,示例中Pecan通过修饰器公开接口。

  这个示例是使用Pecan开发一个具体的Web应用。

from pecan import expose,request, redirect, response
from webob.exc import status_map class BooksController(object):
@expose(content_type='text/plain')
def index(self):
return "Welcome to book section." @expose(content_type='text/plain')
def bestsellers(self):
return "We have 5 books in the top 10." class CatalogController(object):
@expose(content_type='text/plain')
def index(self):
return "Welcome to the catalog." #books
books = BooksController() class RootController(object):

   # 默认页面以index.html为模板
@expose(generic=True, template='index.html')
def index(self):
return dict() # for POST
@index.when(method='POST')
def index_post_test(self, q):
redirect('http://pecan.readthedocs.org/en/latest/search.html?q=%s' % q)

   # show error page
@expose('error.html')
def error(self, status):
try:
status = int(status)
except ValueError: # pragma: no cover
status = 500
message = getattr(status_map.get(status), 'explanation', '')
return dict(status=status, message=message) # /hello
@expose(generic=True, template='index.html')
def hello(self):
return dict() # /testtext.txt
@expose(content_type='text/plain')
def testtext(self):
return 'text test' # json
@expose('json')
def jsontest(self):
response.status = 200
return {'foo': 'bar'} # catalog
catalog = CatalogController()

Full support for REST-style controllers

以下是一个使用Pecan实现的Rest服务.

包括GET,POST,PUT,DELETE.

from pecan import abort, expose

# Note: this is *not* thread-safe.  In real life, use a persistent data store.
BOOKS = {
'': 'The Last of the Mohicans',
'': 'Catch-22'
} class BookController(object): def __init__(self, id_):
self.id_ = id_
assert self.book @property
def book(self):
if self.id_ in BOOKS:
return dict(id=self.id_, name=BOOKS[self.id_])
abort(404) # HTTP GET /<id>/
@expose(generic=True, template='json')
def index(self):
return self.book # HTTP PUT /<id>/
@index.when(method='PUT', template='json')
def index_PUT(self, **kw):
BOOKS[self.id_] = kw['name']
return self.book # HTTP DELETE /<id>/
@index.when(method='DELETE', template='json')
def index_DELETE(self):
del BOOKS[self.id_]
return dict() class RootRestController(object): @expose()
def _lookup(self, id_, *remainder):
return BookController(id_), remainder # HTTP GET /
@expose(generic=True, template='json')
def index(self):
return [dict(id=k, name=v) for k, v in BOOKS.items()] # HTTP POST /
@index.when(method='POST', template='json')
def index_POST(self, **kw):
id_ = len(BOOKS)
BOOKS[id_] = kw['name']
return dict(id=id_, name=kw['name'])

WSGI学习系列Pecan的更多相关文章

  1. WSGI学习系列WebOb

    1. WSGI Server <-----> WSGI Middleware<-----> WSGI Application  1.1 WSGI Server wsgi ser ...

  2. WSGI学习系列Paste

    Paste has been under development for a while, and has lots of code in it. The code is largely decoup ...

  3. WSGI学习系列WSME

    Introduction Web Services Made Easy (WSME) simplifies the writing of REST web services by providing ...

  4. WSGI学习系列多种方式创建WebServer

    def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) ...

  5. WSGI学习系列eventlet.wsgi

    WSGI是Web Service Gateway Interface的缩写. WSGI标准在PEP(Python Enhancement Proposal)中定义并被许多框架实现,其中包括现广泛使用的 ...

  6. 分布式学习系列【dubbo入门实践】

    分布式学习系列[dubbo入门实践] dubbo架构 组成部分:provider,consumer,registry,monitor: provider,consumer注册,订阅类似于消息队列的注册 ...

  7. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

  8. WCF学习系列汇总

    最近在学习WCF,打算把一整个系列的文章都”写“出来,包括理论和实践,这里的“写”是翻译,是国外的大牛写好的,我只是搬运工外加翻译.翻译的不好,大家请指正,谢谢了.如果觉得不错的话,也可以给我点赞,这 ...

  9. EF(Entity Framework)系统学习系列

    好久没写博客了,继续开启霸屏模式,好了,废话不多说,这次准备重新系统学一下EF,一个偶然的机会找到了一个学习EF的网站(http://www.entityframeworktutorial.net/) ...

随机推荐

  1. SpringMvc配置web.xml避免view被dispatcherServlet拦截

    在我们以SpringMvc作为开发框架,开发接口框架时,我们只用到Controller一层,因为数据是交到前端处理的,所以我们是不需要处理视图的.此时,在配置dispatcherServlet时,一般 ...

  2. Spring Bean的装配

    Bean 的装配,即Bean对象的创建.容器根据代码要求创建Bean对象后再传递给代码的过程,称为Bean的装配. 一.默认分的装配方式 默认的装配的方式调用Bean类的构造方法 二.动态工厂Bean ...

  3. mysql字段名与关键字冲突(near "to":syntax error)

    今天遇到个问题,mysql字段为 to ,但是插入时报语法错误,仔细分析to是关键词 使用单引号和双引号 全部失败!!!! 仔细看 mysql的字段都是`(键盘Esc下面那个符号)括起来的, 试了一下 ...

  4. [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 2.

    1. Introduction. 1.1 In part 1 of this series of articles, I explained how managed arrays may be tra ...

  5. select和epoll原理和区别

    对于select和poll,其主要原理跟epoll不同 poll和select的共同点就是,对全部指定设备(fd)都做一次poll,当然这往往都是还没有就绪的,那就会通过回调函数把当前进程注册到设备的 ...

  6. CF D. Fair(思维+DFS)

    http://codeforces.com/contest/987/problem/D 题目大概: 给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产).有k种不同特产. 要求 ...

  7. Windows10上安装OpenStack

    1. 启动Hyper-V 2. 在Hyper-V内安装Ubuntu 这两部可以参考http://www.cnblogs.com/jimmy-y/p/5328306.html(Ubuntu16类似) 3 ...

  8. kohana reading session data error(session_start)引起的错误

    在项目中碰到一个很奇怪的问题,同一个网站目录下两个项目,手机端访问从A项目切换到B项目,总是报错,session_start抛错,后来排查了很久,终于发现是存储session的时候直接存储的对象,导致 ...

  9. 逐行创建、读取并写入txt(matlab) && 生成文件夹里文件名的.bat文件

    fidin=fopen('C:\Users\byte\Desktop\新建文件夹 (4)\tr4.txt','r'); fidout=fopen('C:\Users\byte\Desktop\新建文件 ...

  10. 在MD中使用Emoji

    mark语法中支持emoji表情 具体语法是:emoji: 比如我输入 :smile: 就会出现微笑