FastAPI框架
FastAPI框架
该框架的速度(天然支持异步)比一般的django和flask要快N多倍,号称可以比肩Go
使用该框架需要保证你的python解释器版本是3.6及以上
Ps:django3.X版本也支持异步,但是它的异步功能并没有真正的实现,还有很多bug
安装
pip3 install fastapi
pip3 install unicorn
基本使用
from fastapi import FastAPI
app = FastAPI()
@app.get('/') # 点get就支持get请求
def read_root():
return {"hello":'world'}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app,host='127.0.0.1',port=8080)
模版渲染
fastapi本身是没有模版渲染功能的,需要你借助于第三方的模版工具
该框架默认情况下也是借助于jinja2来做模版渲染
安装jinja2
pip3 install jinja2
基本使用
from starlette.requests import Request
from fastapi import FastAPI
from starlette.templating import Jinja2Templates
app = FastAPI()
# 挂在模版文件夹
tmp = Jinja2Templates(directory='templates')
@app.get('/')
async def get_tmp(request:Request): # async加了就支持异步
return tmp.TemplateResponse('index.html',
{'request':request, # 一定要返回request
'args':'hello world' # 额外的参数可有可无
}
)
@app.get('/{item_id}/') # url后缀
async def get_item(request:Request,item_id):
return tmp.TemplateResponse('index.html',
{'request':request,
'kw':item_id
})
if __name__ == '__main__':
import uvicorn
uvicorn.run(app,host='127.0.0.1',port=8080)
form表单数据交互
基本数据
from starlette.requests import Request
from fastapi import FastAPI,Form
from starlette.templating import Jinja2Templates
app = FastAPI()
tmp = Jinja2Templates(directory='templates')
@app.get('/') # 接受get请求
async def get_user(request:Request):
return tmp.TemplateResponse('form.html',{'request':request})
@app.post('/user/') # 接受post请求
async def get_user(request:Request,
username:str=Form(...), # 直接去请求体里面获取username键对应的值并自动转化成字符串类型
pwd:int=Form(...) # 直接去请求体里面获取pwd键对应的值并自动转化成整型
):
print(username,type(username))
print(pwd,type(pwd))
return tmp.TemplateResponse('form.html',{
'request':request,
'username':username,
'pwd':pwd
})
if __name__ == '__main__':
import uvicorn
uvicorn.run(app,host='127.0.0.1',port=8080)
文件交互
from starlette.requests import Request
from fastapi import FastAPI, Form, File, UploadFile
from starlette.templating import Jinja2Templates
from typing import List
app = FastAPI()
tmp = Jinja2Templates(directory='templates')
@app.get('/') # 接受get请求
async def get_file(request: Request):
return tmp.TemplateResponse('file.html', {'request': request})
# 单个文件
@app.post('/file/') # 接受post请求
async def get_user(request: Request,
file: bytes = File(...),
file_obj: UploadFile = File(...),
info: str = Form(...)
):
return tmp.TemplateResponse('index.html', {
'request': request,
'file_size': len(file),
'file_name': file_obj.filename,
'info':info,
'file_content_type':file_obj.content_type
})
# 多个文件
@app.post('/files/')
async def get_files(request:Request,
files_list:List[bytes] = File(...), # [文件1的二进制数据,文件2的二进制数据]
files_obj_list:List[UploadFile]=File(...) # [file_obj1,file_obj2,....]
):
return tmp.TemplateResponse('index.html',
{'request':request,
'file_sizes':[len(file) for file in files_list],
'file_names':[file_obj.filename for file_obj in files_obj_list]
}
)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='127.0.0.1', port=8080)
静态文件配置
from starlette.staticfiles import StaticFiles
# 挂载静态文件夹
app.mount('/static',StaticFiles(directory='static'),name='static')
# 前端
<link rel="stylesheet" href="{{ url_for('static',path='/css/111.css') }}">
<script src="{{ url_for('static',path='/js/111.js') }}"></script>
FastAPI框架的更多相关文章
- 利用本地HTTPS模拟环境为FastAPI框架集成FaceBook社交三方登录
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_174 提起社交,就不得不说马克·扎克伯格(Mark Zuckerberg)一手创办的社交网络(FaceBook).进入2020年, ...
- FastAPI框架入门 基本使用, 模版渲染, form表单数据交互, 上传文件, 静态文件配置
安装 pip install fastapi[all] pip install unicorn 基本使用(不能同时支持,get, post方法等要分开写) from fastapi import Fa ...
- 三分钟了解 Python3 的异步 Web 框架 FastAPI
快速编码,功能完善.从启动到部署,实例详解异步 py3 框架选择 FastAPI 的原因. FastAPI 介绍 FastAPI 与其它 Python-Web 框架的区别 在 FastAPI 之前,P ...
- 什么是FastAPI异步框架?(全面了解)
一:FastAPI框架 1.FastAPI是应该用于构建API的现代,快速(高性能)的 web 框架,使用Python 3.6+ 并基于标准的 Python 类型提示. 关键性: 快速: 可与Node ...
- FastApi持续更新
FastAPI 框架,高性能,易于学习,高效编码,生产可用 官方文档: https://fastapi.tiangolo.com FastAPI 是一个用于构建 API 的现代.快速(高性能)的 ...
- day02 web主流框架
day02 web主流框架 今日内容概要 手写简易版本web框架 借助于wsgiref模块 动静态网页 jinja2模板语法 前端.web框架.数据库三种结合 Python主流web框架 django ...
- asyncio异步编程【含视频教程】
不知道你是否发现,身边聊异步的人越来越多了,比如:FastAPI.Tornado.Sanic.Django 3.aiohttp等. 听说异步如何如何牛逼?性能如何吊炸天....但他到底是咋回事呢? 本 ...
- 如何利用 Python 爬虫实现给微信群发新闻早报?(详细)
1. 场景 经常有小伙伴在交流群问我,每天的早报新闻是怎么获取的? 其实,早期使用的方案,是利用爬虫获取到一些新闻网站的标题,然后做了一些简单的数据清洗,最后利用 itchat 发送到指定的社群中. ...
- 线下---复习day01
目录 1 个人介绍 2 关于编辑器 3 基础串讲 3.1 解释型和编译型 3.2 数据类型 3.2.1 一切皆对象 3.2.1 深浅copy 3.2.3 可变类型与不可变类型 3.3 字符编码 3.4 ...
随机推荐
- Natas3 Writeup(爬虫协议robots.txt)
Natas3: 页面提示本页面什么都没有. 在源码中发现提示:无信息泄露,谷歌这次不会发现它.提到了搜索引擎,猜测爬虫协议robots.txt中存在信息泄露,访问网站爬虫协议http://natas3 ...
- 14. java基于excel模板导出excel=>使用jxls最新版(注意点)
注意点:如下: jxls官网:http://jxls.sourceforge.net/getting_started.html
- volatile 手摸手带你解析
前言 volatile 是 Java 里的一个重要的指令,它是由 Java 虚拟机里提供的一个轻量级的同步机制.一个共享变量声明为 volatile 后,特别是在多线程操作时,正确使用 volatil ...
- Journal of Proteome Research | Proteomic Profiling of Rhabdomyosarcoma-Derived Exosomes Yield Insights into Their Functional Role in Paracrine Signaling (解读人:孙国莹)
文献名:Proteomic Profiling of Rhabdomyosarcoma-Derived Exosomes Yield Insights into Their Functional Ro ...
- ketika aku 病毒
#客户中了该病毒,本想找病毒样本来看看,可是没找到样本,发现中这个病毒的案例还是相对较少: #国内好像没有对于该病毒没有比较详尽的病毒信息,特此写一下方便后者: #中招表现:目前所能够发现的是能够对浏 ...
- [BFS,大水题] Codeforces 198B Jumping on Walls
题目:http://codeforces.com/problemset/problem/198/B Jumping on Walls time limit per test 2 seconds mem ...
- 推荐系统(Recommender Systems)
本博客是针对Andrew Ng在Coursera上的machine learning课程的学习笔记. 目录 基于内容的推荐(Content-based recommendation) 问题表述 问题范 ...
- 关于利用python进行验证码识别的一些想法
转载:@小五义http://www.cnblogs.com/xiaowuyi 用python加“验证码”为关键词在baidu里搜一下,可以找到很多关于验证码识别的文章.我大体看了一下,主要方法有几类: ...
- Jmeter4.0接口测试之断言实战(六)
在接口测试用例中得有断言,没有断言的接口用例是无效的,一个接口的断言有三个层面,一个是HTTP状态码的断言,另外一个是业务状态码的断言,最后是某一接口请求后服务端响应数据的断言.在Jmeter中增加断 ...
- spring-cloud-gateway动态路由
概述 线上项目发布一般有以下几种方案: 停机发布 蓝绿部署 滚动部署 灰度发布 停机发布 这种发布一般在夜里或者进行大版本升级的时候发布,因为需要停机,所以现在大家都在研究 Devops 方案. 蓝绿 ...