【脚本语言系列】关于PythonWeb服务器Flask,你需要知道的事

本文介绍Flask Web服务器框架的基础用法,包括简单的页面展示、模板使用、URL参数传递及GET参数处理等。Flask以其简洁性和灵活性受到开发者喜爱。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【脚本语言系列】关于PythonWeb服务器Flask,你需要知道的事

如何使用Flask

简单网页

保存demo1.html位于脚本所处的目录下

<!--demo1.html-->
<html>
<head>
<title>Flask Demo1</title>
</head>
<body>
Hello My Friend: {{name}}
</body>
</html>
# -*- coding:utf-8 -*-
from flask import Flask

app = Flask(__name__,static_folder=".", static_url_path="")

@app.route("/")
def home():
    return app.send_static_file("demo1.html")

@app.route("/echo/<name>")
def echo(name):
    return "Hello MyFriend: %s" %name

app.run(port=9999, debug=True)

# http://127.0.0.1:9999/echo/AllenMoore
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 602-361-623
 * Running on http://127.0.0.1:9999/ (Press CTRL+C to quit)

模板运用

保存demo1.html位于脚本所处的目录下的templates文件夹中

<!--demo1.html-->
<html>
<head>
<title>Flask Demo1</title>
</head>
<body>
Hello My Friend: {{name}}
</body>
</html>
# -*- coding:utf-8 -*-
from flask import Flask,render_template

app = Flask(__name__)

@app.route("/echo/<name>")
def echo(name):
    return render_template("demo1.html",name=name)

app.run(port=9999, debug=True)

# http://127.0.0.1:9999/echo/AllenMoore
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 602-361-623
 * Running on http://127.0.0.1:9999/ (Press CTRL+C to quit)
  • URL的扩展作传参
    保存demo2.html位于脚本所处的目录下的templates文件夹中
<!--demo2.html-->
<html>
<head>
<title>Flask Demo2</title>
</head>
<body>
Hello My Friend: {{name}}
Welcome To This Place: {{place}}
</body>
</html>
# -*- coding:utf-8 -*-
from flask import Flask,render_template

app = Flask(__name__)

@app.route("/echo/<name>/<place>")
def echo(name, place):
    return render_template("demo2.html", name=name, place=place)

app.run(port=9999, debug=True)

# http://127.0.0.1:9999/echo/AllenMoore/TheMachine
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 602-361-623
 * Running on http://127.0.0.1:9999/ (Press CTRL+C to quit)
  • GET参数作传参

  • URL的扩展作传参
    保存demo3.html位于脚本所处的目录下的templates文件夹中

<!--demo2.html-->
<html>
<head>
<title>Flask Demo2</title>
</head>
<body>
Hello My Friend: {{name}}
Welcome To This Place: {{place}}
</body>
</html>
# -*- coding:utf-8 -*-
from flask import Flask,render_template,request

app = Flask(__name__)

@app.route("/echo/")
def echo():
    kwargs={}
    kwargs["name"] = request.args.get("name")
    kwargs["place"] = request.args.get("place")
    return render_template("demo3.html", **kwargs)

app.run(port=9999, debug=True)

# http://127.0.0.1:9999/echo/?name=AllenMoore&place=TheMachine
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 602-361-623
 * Running on http://127.0.0.1:9999/ (Press CTRL+C to quit)

什么是Flask

Flask是Web服务器框架,定位于入门级。

为何使用Flask

Flask如同Bottle一样简单,而且还能扩展许多扩展功能,比如认证服务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值