【Python Web】Pecan学习:官方文档解读

本文介绍了Pecan这一轻量级Python Web框架,它提供对象调度式路由,专注于HTTP本身。具备对象调度、支持REST控制器等特性,还介绍了创建应用、配置文件、路由算法等内容,以及开发、部署和测试相关知识,可通过WSGI兼容服务器部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduce to pecan

What is pecan?

Pecan was created to fill a void in the Python web-framework world – a very lightweight framework that provides object-dispatch style routing. Pecan does not aim to be a “full stack” framework, and therefore includes no out of the box support for things like sessions or databases. Pecan instead focuses on HTTP itself.

What are its characteristics?

Although it is lightweight, Pecan does offer an extensive feature set for building HTTP-based applications, including:
Object-dispatch for easy routing

  • Full support for REST-style controllers
  • Extensible security framework
  • Extensible template language support
  • Extensible JSON support
  • Easy Python-based configuration

While Pecan doesn’t provide support for sessions or databases out of the box, tutorials are included for integrating these yourself in just a few lines of code.

https://pecan.readthedocs.io/en/latest/index.html

Create the first pecan application

pecan application

Run the pecan application

Run pecan

Pecan configuration file

configuration file
config.py
app.py

Entry to the application

The Root Controller is the entry point for your application. You can think of it as being analogous to your application’s root URL path (in our case, http://localhost:8080/).
RootController

Controllers and Routing

controller and routing
expose

Pecan’s Routing Algorithm

Routing Algorithm

Interacting with the Request and Response Object

Interacting with the Request and Response Object

Templating in Pecan

Templating in Pecan

Writing RESTful Web Services

with Generic Controllers

Pecan simplifies RESTful web services by providing a way to overload URLs based on the request method. For most API’s, the use of generic controller definitions give you everything you need to build out robust RESTful interfaces (and is the recommended approach to writing RESTful web services in pecan).
Generic Controllers

With RestController

With RestController
default method with RestController
TurboGears 2 is built on top of the experience of several next generation web frameworks including TurboGears 1 (of course), Django, and Rails.

Security and Authentication

Security and Authentication
Multiple Secure Controllers

Pecan Hooks

Pecan Hooks
Attaching Hooks

JSON Serialization

JSON Serialization

Context/Thread-Locals vs. Explicit Argument Passing

Thread-locals
Disabling Thread-Local Proxies

Command Line Pecan

Command Line Pecan
Command Line Pecan

Logging & Testing

Logging

Logging
Loggings

Testing

  • Tests can live anywhere in your Pecan project as long as the test runner can discover them.
  • In particular, Pecan projects are known to work well with nose, pytest, and tox.
  • Writing Functional Tests with WebTest
    Testing

Developing locally & Deploying in Production

Developing Locally

Developing Locally

Deploying

Deploying
Pecan and WSGI
WSGI是一个Python标准,它描述了服务器和应用程序之间的标准接口。任何Pecan应用程序也被称为“WSGI应用程序”,因为它实现了WSGI接口,所以任何与WSGI兼容的服务器都可以用来服务于您的应用程序。
Common Recipes
Apache+mod_wsgi

  • mod wsgi是一个流行的Apache模块,可以用来托管任何与wsgi兼容的Python应用程序(包括Pecan应用程序)。
  • uwsgi是一个用纯C编写的快速、自我修复和开发人员/系统管理员友好的应用程序容器服务器。
  • Gunicorn或“greenunicorn”是一个用于UNIX的wsgihttp服务器。它是从Ruby的Unicorn项目移植的fork-worker前的模型。它支持eventlet和greenlet。
  • cherrypy提供了一个纯pythonhttp/1.1兼容的WSGI线程池web服务器。

References

  1. https://pecan.readthedocs.io/en/latest/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

镰刀韭菜

看在我不断努力的份上,支持我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值