Flask 学习笔记1 --Qickstart
来源:互联网 发布:紫青双剑仙羽进阶数据 编辑:程序博客网 时间:2024/06/04 18:59
A Basic WSGI Introduction
Werkzeug is a utility library for WSGI(Web Server GateWay Interface). WSGI itself is a protocol or convention that ensures that your web application can speak with the webserver and more importantly that web applications work nicely together.
1. A basic “Hello World” application in WSGI without the help of Werkzeug:
2. SAE Plateform Flask application:
3. Here is how you would write that application with response objects:
Eager to get started?
Our URL is:http://simonrestful.applinzi.com:80/
We use the route()
decorator to tell Flask what URL should trigger our function.
To stop the server, hit control-C. However, the server is free!
4. To add variable parts to a URL you can mark these special sections as <variable_name>
. Such a part is then passed as a keyword argument to your function.you alse can use <converter:variable_name>
.
The following converters exist:
5. To build a URL to a specific function you can use the url_for()
function. It accepts the name of the function as first argument and a number of keyword arguments.
There are three good reasons for this:
- Reversing is often more descriptive than hard-coding the URLs. More importantly, it allows you to change URLs in one go, without having to remember to change URLs all over the place.
- URL building will handle escaping of special characters and Unicode data transparently for you, so you don’t have to deal with them.
- If your application is placed outside the URL root (say, in
/myapplication
instead of/
),url_for()
will handle that properly for you.
6. HTTP (the protocol web applications are speaking) knows different methods for accessing URLs.
(1)从网页访问使用的是GET方法;
(2)使用curl命令,以POST方式访问
(3)Python 客户端,以POST方式访问
7. The HTTP method (also often called “the verb(动作)”) tells the server what the clients wants to do with the requested page. The following methods are very common:
(1) GET : The browser tells the server to just get the information stored on that page and send it.
(2) HEAD : The browser tells the server to get the information, but it is only interested in theheaders, not the content of the page.
(3) POST : The browser tells the server that it wants to post some new information to that URL and that the server must ensure the data is stored and only stored once.
(4) PUT : Similar to POST but the server might trigger the store procedure multiple times by overwriting the old values more than once.(预防掉线)
(5) DELETE : Remove the information at the given location.
(6) OPTIONS : Provides a quick way for a client to figure out which methods are supported by this URL. Starting with Flask 0.6, this is implemented for you automatically.
8. Dynamic web applications also need static files.Flask can do that as well. Just create a folder called staticin your package or next to your module and it will be available at /static on the application.
To generate URLs for static files, use the special 'static'
endpoint name:(从错误消息里看prex)
8. Accessing Request Data
For web applications it’s crucial to react to the data a client sent to the server. In Flask this information is provided by the globalrequest
object. The current request method is available by using the method
attribute. To access form data (data transmitted in a POST or PUT request) you can use the form
attribute.
9. To access parameters submitted in the URL (?key=value
) you can use the args
attribute
10. File Upload
You can handle uploaded files with Flask easily. Uploaded files are stored in memory or at a temporary location on the filesystem. You can access those files by looking at the files
attribute on the request object.
Each uploaded file is stored in that dictionary. It behaves just like a standard Python file
object, but it also has a save()
method that allows you to store that file on the filesystem of the server.
用法?
11. To redirect a user to another endpoint, use the redirect()
function.
- Flask 学习笔记1 --Qickstart
- Flask学习笔记-1
- Flask学习笔记 -- 1
- Flask web学习笔记1
- python flask 学习笔记1 简单入门
- Flask-SQLALchemy学习笔记
- Flask源码学习笔记
- flask学习笔记
- flask学习笔记(-模板)
- flask学习笔记(-数据库)
- flask学习笔记
- Flask--学习笔记-注册
- Flask学习笔记
- Flask 学习笔记
- Flask学习笔记-- 2
- Flask学习笔记-- 3
- Flask学习笔记-- 4
- Flask学习笔记--5
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation
- Invocation of destroy method 'close' failed on bean with name 'sqlSession'
- 浅谈跨域中cookie和session的联系
- VS2015下安装VTK7.0错误总结
- Node.js模块系统
- Flask 学习笔记1 --Qickstart
- 【2042】模拟开关
- C#控制台 static与const不能同时修饰一个字段
- pycharm导入第三方依赖
- 面向对象七大设计原则
- Thinkphp框架的源码通读2——核心Think类从开始到加载方法
- 第六届蓝桥杯B组---7.牌型总数
- CODEFORCES 454C Little Pony and Expected Maximum
- HDU-1806 Frequent values(RMQ)