全部的Flask程序都必须建立一个程序实例,程序实例是Flask类的对象html
from flask import Flask app = Flask(__name__)Flask 类的构造函数Flask()只有一个必须指定的参数,即程序主模块或包的名字。在大多数程序中,python的
__name__
变量就是所需的值。(Flask这个参数决定程序的根目录,以便稍后可以找到相对与程序根目录的资源文件位置)——《Flask Web开发》python
1.路由:flask
我的对路由的理解:程序实例须要知道每一个URL请求运行那些代码,用route()修饰器把函数绑定到URL上浏览器
2.视图函数:服务器
返回的响应能够是字符串或者复杂的表单app
变量规则 Flask中文文档(https://dormousehole.readthedocs.io/en/latest/quickstart.html)
经过把 URL 的一部分标记为
<variable_name>
就能够在 URL 中添加变量。标记的 部分会做为关键字参数传递给函数。经过使用<converter:variable_name>
,能够 选择性的加上一个转换器,为变量指定规则。请看下面的例子:框架@app.route('/user/<username>') def show_user_profile(username): # show the user profile for that user return 'User %s' % escape(username) @app.route('/post/<int:post_id>') def show_post(post_id): # show the post with the given id, the id is an integer return 'Post %d' % post_id @app.route('/path/<path:subpath>') def show_subpath(subpath): # show the subpath after /path/ return 'Subpath %s' % escape(subpath)转换器类型:函数
string
(缺省值) 接受任何不包含斜杠的文本 int
接受正整数 float
接受正浮点数 path
相似 string
,但能够包含斜杠(与string区分)uuid
接受 UUID 字符串 ! 注意:
<converter:variable_name>
中间不能有空格,格式要一致post
if __name__ == '__main__': app.run(debug=True)
1.服务器启动之后会进入轮询,等待并处理请求(轮询是用来解决服务器压力过大的问题的。若是保持多个长链接,服务器压力会过大,所以。专门创建一个轮询请求的接口,里面只保留一个任务id,只须要发送任务id,就能够获取当前任务的状况。若是返回告终果,轮询结束,没有返回则等待一下子,继续发送请求。 )学习
2.把debug参数设置为True,启用调试模式
3.在浏览器上输入网址,若是是生成的初始网址,而且URL带了变量,这时先会返回错误,由于生成的初始网址里面没有变量,例如应该加上/user/44654。其中的<>也要去掉