Sanic基准测试
uvloop
安装 uvloop
pip install uvloop
import asyncio
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
安装 Sanic
pip install sanic
创建第一个 sanic 代码
from sanic import Sanic
from sanic.response import text
app = Sanic(__name__)
@app.route("/")
async def test(request):
return text('Hello world!')
app.run(host="0.0.0.0", port=8000, debug=True)
路由(Routing)
@app.route('/')
def index():
return text('Index Page')
@app.route('/hello')
def hello():
return text('Hello World')
变量规则
from sanic.response import text
@app.route('/tag/<tag>')
async def tag_handler(request, tag):
return text('Tag - {}'.format(tag))
@app.route('/number/<integer_arg:int>')
async def integer_handler(request, integer_arg):
return text('Integer - {}'.format(integer_arg))
@app.route('/number/<number_arg:number>')
async def number_handler(request, number_arg):
return text('Number - {}'.format(number_arg))
@app.route('/person/<name:[A-z]>')
async def person_handler(request, name):
return text('Person - {}'.format(name))
@app.route('/folder/<folder_id:[A-z0-9]{0,4}>')
async def folder_handler(request, folder_id):
return text('Folder - {}'.format(folder_id))
HTTP 请求类型
from sanic.response import text
@app.route('/post', methods=['POST'])
async def post_handler(request):
return text('POST request - {}'.format(request.json))
@app.route('/get', methods=['GET'])
async def get_handler(request):
return text('GET request - {}'.format(request.args))
from sanic.response import text
@app.post('/post')
async def post_handler(request):
return text('POST request - {}'.format(request.json))
@app.get('/get')
async def get_handler(request):
return text('GET request - {}'.format(request.args))
add_route 方法
from sanic.response import text
# Define the handler functions
async def handler1(request):
return text('OK')
async def handler2(request, name):
return text('Folder - {}'.format(name))
async def person_handler2(request, name):
return text('Person - {}'.format(name))
# Add each handler function as a route
app.add_route(handler1, '/test')
app.add_route(handler2, '/folder/<name>')
app.add_route(person_handler2, '/person/<name:[A-z]>', methods=['GET'])
URL 构建
@app.route('/')
async def index(request):
# generate a URL for the endpoint `post_handler`
url = app.url_for('post_handler', post_id=5)
# the URL is `/posts/5`, redirect to it
return redirect(url)
@app.route('/posts/<post_id>')
async def post_handler(request, post_id):
return text('Post - {}'.format(post_id))
url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
# /posts/5?arg_one=one&arg_two=two
# 支持多值参数
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
# /posts/5?arg_one=one&arg_one=two
使用蓝图(Blueprint)
-
把一个应用分解为一套蓝图。这是针对大型应用的理想方案:一个项目可以实例化一个 应用,初始化多个扩展,并注册许多蓝图。
-
在一个应用的 URL 前缀和(或)子域上注册一个蓝图。 URL 前缀和(或)子域的参数 成为蓝图中所有视图的通用视图参数(缺省情况下)。
-
使用不同的 URL 规则在应用中多次注册蓝图。
-
通过蓝图提供模板过滤器、静态文件、模板和其他工具。蓝图不必执行应用或视图 函数。
blueprint 示例
from sanic import Sanic
from sanic.response import json
from sanic import Blueprint
bp = Blueprint('my_blueprint')
@bp.route('/')
async def bp_root(request):
return json({'my': 'blueprint'})
app = Sanic(__name__)
app.blueprint(bp)
app.run(host='0.0.0.0', port=8000, debug=True)
使用蓝图注册全局中间件
@bp.middleware
async def print_on_request(request):
print("I am a spy")
@bp.middleware('request')
async def halt_request(request):
return text('I halted the request')
@bp.middleware('response')
async def halt_response(request, response):
return text('I halted the response')
使用蓝图处理异常
@bp.exception(NotFound)
def ignore_404s(request, exception):
return text("Yep, I totally found the page: {}".format(request.url))
使用蓝图处理静态文件
bp.static('/folder/to/serve', '/web/path')
使用url_for
@blueprint_v1.route('/')
async def root(request):
url = app.url_for('v1.post_handler', post_id=5) # --> '/v1/post/5'
return redirect(url)
@blueprint_v1.route('/post/<post_id>')
async def post_handler(request, post_id):
return text('Post {} in Blueprint V1'.format(post_id))
操作请求数据
为什么不像Flask 一样提供一个全局变量 request?
Flask 是同步请求,每次请求都有一个独立的新线程来处理,这个线程中也只处理这一个请求。而Sanic是基于协程的处理方式,一个线程可以同时处理几个、几十个甚至几百个请求,把request作为全局变量显然会比较难以处理。
json(any) json body
from sanic.response import json
@app.route("/json")
def post_json(request):
return json({ "received": True, "message": request.json })
args(dict) URL请求参数
{'key1': ['value1'], 'key2': ['value2']}
raw_args(dict) 和args 类似
{'key1': 'value1', 'key2': 'value2'}
form(dict)处理 POST 表单请求,数据是一个字典
body(bytes)处理POST 表单请求,数据是一个字符串
-
file
-
ip
-
app
-
url
-
scheme
-
path
-
query_string
关于响应
-
文本
response.text('hello world')
-
html
response.html('<p>hello world</p>')
-
json
response.json({'hello': 'world'})
-
file
response.file('/srv/www/hello.txt')
-
streaming
from sanic import response
@app.route("/streaming")
async def index(request):
async def streaming_fn(response):
response.write('foo')
response.write('bar')
return response.stream(streaming_fn, content_type='text/plain')
-
redirect
response.file('/json')
-
raw
response.raw('raw data')
-
如果想修改响应的headers可以传入headers 参数
from sanic import response
@app.route('/json')
def handle_request(request):
return response.json(
{'message': 'Hello world!'},
headers={'X-Served-By': 'sanic'},
status=200
)
配置管理
配置入门
app = Sanic('myapp')
app.config.DB_NAME = 'appdb'
app.config.DB_USER = 'appuser'
db_settings = {
'DB_HOST': 'localhost',
'DB_NAME': 'appdb',
'DB_USER': 'appuser'
}
app.config.update(db_settings)
从对象导入配置
import myapp.default_settings
app = Sanic('myapp')
app.config.from_object(myapp.default_settings)
使用配置文件
app = Sanic('myapp')
app.config.from_envvar('MYAPP_SETTINGS')
$ export MYAPP_SETTINGS=/path/to/config_file
$ python myapp.py
部署
使用 supervisord 部署
supervisord -c supervisor.conf
总结
相关推荐
- **微框架概念**:“Micro”在这里指的是Flask作为一款轻量级(或称为微型)Web框架的概念。 - **设计哲学**:Flask的设计理念是“轻巧”,它没有内置数据库抽象层、表单验证等功能,这些功能可以通过扩展来实现。 ...
Django是一个高级的Python Web框架,鼓励快速开发和干净、实用的设计。Flask是一个用Python编写的轻量级Web应用框架,它也遵循了"约定优于配置"的原则,但比Django更加灵活。 讲义中提到的洪强宁是一位经验丰富的...
1. **Web应用开发**:Python中的Django和Flask等框架使得构建Web应用变得高效快捷。 2. **操作系统管理与服务器运维自动化**:通过模块如os、sys、subprocess等,可以方便地进行系统管理和自动化任务。 3. **科学...
### Python快速入门知识点详解 #### 一、Python简介与特色 - **简介**:Python是一种高级编程语言,因其简洁易读的语法而受到广泛欢迎。无论是编程新手还是经验丰富的开发者,都能快速上手并利用Python解决复杂的...
例如,Web开发中常用的框架有Django和Flask;科学计算领域有NumPy、Pandas和SciPy等库;在游戏开发中,虽然Python不是首选,但也有Pygame等库支持。 Python的特点包括但不限于: 1. 简洁明了的语法,易于学习和理解...
1. **Web开发**:通过Django或Flask框架学习构建Web应用,包括路由、模板、数据库交互等。 2. **数据分析**:使用Pandas库进行数据清洗、预处理,Numpy进行数值计算,Matplotlib和Seaborn进行数据可视化。 3. **...
### Python技术快速入门指南 #### 一、Python的基本概念与特点 Python 是一门现代的、通用的编程语言,以其简洁的语法和强大的功能受到广大程序员的喜爱。作为一种解释型语言,Python 不需要编译就能直接运行,这...
### Python基础入门资料 #### Python语言介绍 **1.1 Python起源** Python 由荷兰程序员吉多·范罗苏姆(Guido van Rossum)在1989年圣诞节期间开始设计开发。他最初的想法是创建一个新的脚本解释程序,以此来继承...
最后,书中的第三个项目是创建和定制一个简单的Web应用,可能涵盖基础的Web框架如Flask或Django,让读者体验Web开发的基本流程。 这本书适合对Python感兴趣的所有层次的读者,无论是初学者还是有一定经验的开发者,...
Python 的设计哲学强调代码的可读性和简洁的语法,同时拥有丰富的标准库和第三方库支持,使得 Python 在多个领域都有广泛的应用,如 Web 开发、数据分析、人工智能、网络爬虫等。 ### Python 版本介绍 Python 目前...
Web开发方面,Django和Flask框架简化了Web应用的构建,BeautifulSoup和Scrapy等库则助力Web爬虫的编写。此外,Python在自动化脚本编写中表现出色,能提高工作效率。 为了进一步学习Python,除了阅读本文档,你还应...
Python从入门到实践第三版源码+练习+PPT是一套全面覆盖Python编程基础知识的资源包。它不仅提供了Python的实战源码,还包括了配套的练习题以及教学PPT,非常适合初学者构建扎实的编程基础。 首先,资源包中的源码...
Python毕业生信息审核...总之,Python毕业生信息审核系统源码是一个集成了Python编程、Web框架、数据库管理、用户界面设计、工作流管理、安全性和测试等多方面知识的项目,对于学习和提升Python Web开发技能大有裨益。
python入门到高级全栈工程师培训视频学习资料;本资料仅用于学习,请查看后24小时之内删除。 【课程内容】 第1章 01 计算机发展史 02 计算机系统 03 小结 04 数据的概念 05 进制转换 06 原码补码反码 07 物理层和...
这只是Python语言基础的冰山一角,后续章节将涵盖运算符与表达式、流程控制语句、列表与元组、字典与集合、字符串处理、正则表达式、函数、面向对象编程、模块、异常处理、文件操作、数据库操作、GUI编程、网络爬虫...
3. **Web开发**:Django和Flask是流行的Python Web框架,用于构建高效且灵活的Web应用。 4. **自动化脚本**:Python可以用于系统管理任务,如文件操作、定时任务等。 5. **人工智能**:利用TensorFlow、Keras或...
Django和Flask等Web框架则在互联网应用开发中扮演着重要角色;TensorFlow、PyTorch等深度学习库也被广大数据科学家和机器学习工程师所青睐。 在Python中,开发者可以编写非常简洁的代码来实现复杂的逻辑。例如,在...
Flask 是一个基于 Python 的轻量级 Web 应用框架,它以简洁著称,非常适合初学者快速上手并进行实际项目开发。Flask 本身并不包含太多额外的功能,这种设计使得开发者可以根据自己的需求自由选择第三方库来增强应用...
Python 3.7.3是3.7系列的第三次重大维护更新,旨在提供更好的稳定性和错误修复。 2. **64位支持**:Python 3.7.3的64位版本允许程序处理超过4GB的数据,这对于大数据分析、机器学习和高性能计算等场景至关重要。在...