Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0569
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在:
- 个人网站: `https://oeasy.org`
- 蓝桥云课: `https://www.lanqiao.cn/courses/3584`
- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
- Gitee: `https://gitee.com/overmind1980/oeasypython`
---

mkdir myproject
cd myproject
vi app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Index Page'
@app.route('/hello')
def hello():
return 'Hello, World'
if __name__ == "__main__":
app.run(debug=True,host="0.0.0.0",port=8081)
nohup python3 app.py >> flask.log 2>&1 &
firefox http://127.0.0.1:8081/hello http://127.0.0.1:8081/ &







- url中 的 name
- 可以是 任意字符串
- 所以是 动态的
- 动态路径 的 路由
from flask import Flask
from markupsafe import escape
app = Flask(__name__)
@app.route("/<name>")
def hello(name):
return f"Hello, {escape(name)}!"
if __name__ == "__main__":
app.run(debug=True,host="0.0.0.0",port=8081)
firefox http://127.0.0.1:8081/oeasy &

from flask import Flask
from markupsafe import escape
app = Flask(__name__)
@app.route("/<name>")
def hello(name):
len_of_name = len(name)
if len_of_name > 5:
return "name is long"
elif len_of_name <= 4:
return "name is short"
else:
return f"Hello, {escape(name)}!"
if __name__ == "__main__":
app.run(debug=True,host="0.0.0.0",port=8081)
firefox http://127.0.0.1:8081/oeasyo

firefox http://127.0.0.1:8081/o &

firefox http://127.0.0.1:8081/oeasy &

- 我们看 视频的时候
- 要明确 某个BV号
- 根据BV号 访问视频相关的 内容


from flask import Flask
from markupsafe import escape
app = Flask(__name__)
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return f'User {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 f'Post {post_id}'
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
# show the subpath after /path/
return f'Subpath {escape(subpath)}'
if __name__ == "__main__":
app.run(debug=True,host="0.0.0.0",port=8081)
| 路由类型 |
路由定义语法 |
匹配规则说明 |
示例 URL 及对应处理逻辑 |
| 字符串地址(无约束) |
/user/ |
匹配任意字符串(除 / 外 ) |
如 localhost:5000/user/oeasy ,username 为 oeasy ,返回 User oeasy |
| 整数类型约束 |
/post/int:post_id |
仅匹配整数形式内容 |
如 localhost:5000/post/123 ,post_id 为 123 ,返回 Post 123 |
| 路径类型约束 |
/path/path:subpath |
匹配包含 / 的完整路径片段 |
如 localhost:5000/path/a/b/c ,subpath 为 a/b/c ,返回 Subpath a/b/c |
- 字符串地址
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return f'User {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 f'Post {post_id}'

- 子路径地址
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
# show the subpath after /path/
return f'Subpath {escape(subpath)}'

if __name__ == "__main__":
print(app.url_map)
app.run(debug=True,host="0.0.0.0",port=8081)



nohup python3 app.py >> flask.log 2>&1 &

| 路由类型 |
路由定义语法 |
匹配规则说明 |
示例 URL 及对应处理逻辑 |
| 字符串地址(无约束) |
/user/ |
匹配任意字符串(除 / 外 ) |
如 localhost:5000/user/oeasy ,username 为 oeasy ,返回 User oeasy |
| 整数类型约束 |
/post/int:post_id |
仅匹配整数形式内容 |
如 localhost:5000/post/123 ,post_id 为 123 ,返回 Post 123 |
| 路径类型约束 |
/path/path:subpath |
匹配包含 / 的完整路径片段 |
如 localhost:5000/path/a/b/c ,subpath 为 a/b/c ,返回 Subpath a/b/c |
- 大型应用 有各种 要处理的url
- 都要 放 app.py 里
- 能分门别类 放到 不同py文件 中吗??
- 下次再说👋🏻
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。