Flask框架总结

news2024/10/1 19:37:17

python的web框架 Flask

参考: https://dormousehole.readthedocs.io/en/2.1.2/tutorial/factory.html

一、启动脚本

随便编写一个py文件,如test.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello Flask!</p>"

如果没有写

if __name__ == '__main__':
    app.run()

方式一:

修为为app.py或者wsgi.py

方式二:

使用命令flask --app test.py run

方式三:

加上

if __name__ == '__main__':
    app.run()

二、url_for()参数问题

url_for()将函数名转为对应的url请求地址

from flask import url_for

app = Flask(__name__)

@app.route('/')
def index():
    return 'index'

@app.route('/login')
def login():
    return 'login'

@app.route('/user/<username>')
def profile(username):
    return f'{username}\'s profile'

with app.test_request_context():
    print(url_for('index'))
    print(url_for('login'))
    print(url_for('login', next='/'))
    # 对于变量类型的url,需要在第二个参数位置传递参数名
    print(url_for('profile', username='John Doe'))
    
if __name__ == '__main__':
    app.run(debug=True)

app.test_request_context()有啥用?

用于测试使用, 例如测试发送一个请求地址为/login,请求方式为GET的请求

with app.test_request_context('/login', method='GET'):
    print("测试请求到来...")
    # 对测试的请求地址和方式进行断言,如果断言不通过,就会报错
    assert request.path == '/login'
    assert request.method == 'GET'

三、静态html文件中引入css/js/image等资源

在html中使用{{url_for("参数1","参数2")}}

  • 参数1: 表示static静态文件目录
  • 参数2: 表示要引入的文件, 格式: filename=“xxx”

说明: 文件层级结构为

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

测试html

<!--<img src="{{url_for('static',filename='test.jpg')}}" alt="">-->
<!-- 等同于 -->
<img src="/static/test.jpg" alt="">

</body>
</html>

四、是否对html格式的字符串原样解析

关键方法: Markup()

from markupsafe import Markup

五、获取请求数据: request.args、request.form

request.args

  • 获取url后?号后面的参数如: http://127.0.0.1:5000/login?info=shanghai&address=BJ

    使用request.args获取到的是info=shanghai&address=BJ这部分数据

  • 无论此时的请求方式是POST还是GET,request.args获取到的永远都是url中?后面的那部分

request.form

  • 获取的是body中的数据(如form表单提交)
  • 无论此时的请求方式是GET还是POST,request.form获取的永远都是body中的数据
from flask import Flask, request

app = Flask(__name__)

@app.route("/login", methods=['GET', 'POST'])
def login():
    print(request.form)
    print(request.args)
    return "hello!"


if __name__ == '__main__':
    app.run(debug=True)

① GET请求时, 分别设置Params参数和Body参数, 使用postman测试如下:

展示GET请求Params部分请求参数展示GET请求Body部分请求参数
在这里插入图片描述在这里插入图片描述

响应结果如下:

ImmutableMultiDict([('sex', 'nan')])
ImmutableMultiDict([('info', 'shanghai'), ('address', 'BJ')])

② POST请求时, 分别设置Params参数和Body参数, 使用postman测试如下:

展示POST请求Params部分请求参数展示POST请求Body部分请求参数
在这里插入图片描述在这里插入图片描述

响应结果如下:

ImmutableMultiDict([('sex', 'nan')])
ImmutableMultiDict([('info', 'shanghai'), ('address', 'BJ')])

六、重定向页面,终止页面以及自定义404等错误页面

①重定向页面

from flask import Flask, request, render_template, make_response, redirect, abort
from werkzeug.utils import secure_filename

app = Flask(__name__)

@app.route("/")
def index():
    # 重定向
    return redirect(url_for("showInfo"))


@app.route("/info")
def showInfo():
    return "info信息..."


if __name__ == '__main__':
    app.run(debug=True)

②终止程序执行

from flask import Flask, request, render_template, make_response, redirect, abort
from werkzeug.utils import secure_filename

app = Flask(__name__)

@app.route("/")
def index():
    abort(404)
    
if __name__ == '__main__':
    app.run(debug=True)

③自定义错误页面

from flask import Flask, request, render_template, make_response, redirect, abort,url_for
from werkzeug.utils import secure_filename

app = Flask(__name__)


@app.route("/")
def index():
    abort(404)

"""
当访问 http://localhost:5000/ 时,会先走index方法, index方法终止,并响应404, 404码会被标注了@app.errorhandler(404)注解的方法捕获,最终走自定义的404页面
"""
@app.errorhandler(404)
def page_not_found(error):
    # 自定义错误页面
    return render_template("404.html",error=error),404

if __name__ == '__main__':
    app.run(debug=True)

七、使用session时,需要执行secret_key

from flask import Flask, session, redirect, url_for, request

app = Flask(__name__)
"""
设置 secret_key,可以使用如下方式生成:
import secrets
print(secrets.token_hex())进行生成
"""
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'


@app.route('/')
def index():
    if 'username' in session:
        return f'Logged in as {session["username"]}'
    return 'You are not logged in'


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('index'))
    return '''
        <form method="post">
            <p><input type=text name=username>
            <p><input type=submit value=Login>
        </form>
    '''


@app.route('/logout')
def logout():
    # remove the username from the session if it's there
    session.pop('username', None)
    return redirect(url_for('index'))


if __name__ == '__main__':
    app.run(debug=True)

八、日志工具

app.logger

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    app.logger.debug("debug信息...")
    app.logger.warning("提醒信息")
    app.logger.error("出错了..")
    return "hello"

if __name__ == '__main__':
    app.run(debug=True)

九、关于Flask()的参数问题

class Flask(Scaffold):
    # ...省略...
    
    # 构造方法如下,着重看下初始化参数含义:
	def __init__(
        self,
        import_name: str,
        static_url_path: t.Optional[str] = None,
        static_folder: t.Optional[t.Union[str, os.PathLike]] = "static",
        static_host: t.Optional[str] = None,
        host_matching: bool = False,
        subdomain_matching: bool = False,
        template_folder: t.Optional[t.Union[str, os.PathLike]] = "templates",
        instance_path: t.Optional[str] = None,
        instance_relative_config: bool = False,
        root_path: t.Optional[str] = None,
    ):

参数说明

  • import_name: 值flask应用所在的包名或模块名, 一般为 __name__

  • static_folder: 默认值为 static, 指的是静态文件所在的目录; 如改为assets,则需要将静态资源文件css/js/images等放到assets目录下

  • static_url_path: 指的是会在页面静态资源(图片,css,js)文件展示的时候,会在路径前面追加上指定的字符;

    举例: 在页面显示一张图片时,html代码为 <img src="{{url_for('static',filename='test.jpg')}}" alt="">

    渲染到页面上后,为 http://localhost:5000/test.jpg ; 如果设置了: static_url_path=“/abc/def”, 则页面渲染后变成了 http://localhost:5000/abc/def/test.jpg

  • template_folder: 指定模板html的路径,默认为templates

十、蓝图 - Blueprint

Flask框架的蓝图(Blueprint)类似与PHP的控制器,如用户控制器UserController.php,商品控制器GoodsController.php; 从对应关系上来说, 一个蓝图(Blueprint)对应一个控制器; 如果没有蓝图概念的话,估计需要将所有的处理页面请求的逻辑代码写到一个py文件(如: main.py)中, 有了蓝图以后就可以对不同的请求进行拆分了: 用户模块的前端请求逻辑写到用户蓝图(如: User.py)中; 商品模块的前端请求逻辑写到商品蓝图(如: Goods.py)中. 最后在将写好的蓝图注册到主应用(如: main.py, 主应用就是写有app=Flask(__name__)并启动(app.run())了的那个py文件)中就行了

举例: 有两个模块: 一个是用户鉴权模块,需要登录和注册(auth.py); 另外一个是网站的其他普通页面(main.py)

auth.py 是一个蓝图

from flask import Flask, Blueprint, render_template

auth_bp = Blueprint("auth", __name__, url_prefix="/auth")


@auth_bp.route("/register")
def user_register():
    print("用户注册...")
    return render_template("register.html")

@auth_bp.route("/login")
def user_login():
    print("用户登录...")
    return render_template("login.html")

"""
@auth_bp.before_app_request注解在其他方法之前先调用, 可以用它做统一的校验,类似与`前置方法`,
类似的注解, Flask框架提供的还有很多
"""
@auth_bp.before_app_request
def check():
    print("Before request 被调用了...")

main.py 应用实例文件

from flask import Flask, render_template
import auth

app = Flask(__name__)


@app.route("/")
@app.route("/index")
def index():
    print("网站首页...")
    return render_template("index.html")


if __name__ == '__main__':
    
    """在服务启动前将auth.py蓝图注册进来"""
    from auth import auth_bp
    app.register_blueprint(auth_bp)

    app.run(debug=True)

浏览器访问: http://localhost:5000, 响应结果如下:

index首页面

浏览器访问: http://localhost:5000/auth/login, 响应结果如下:

登录页面

浏览器访问: http://localhost:5000/auth/register, 响应结果如下:

注册页面

使用蓝图起到了不同的逻辑分文件编写的作用

十一、将项目打包成.whl文件

参考: https://dormousehole.readthedocs.io/en/2.1.2/tutorial/install.html

大致过程如下:

  • 在开发完成的项目根目录中书写setup.py文件和MANIFEST.in文件

    例如你可以按照下列的demo样例进行书写, 详细的书写规则请百度搜索:

    setup.py

    from setuptools import find_packages, setup
    
    setup(
        name='MyFlaskProject',
        version='1.0.0',
        packages=find_packages(),
        include_package_data=True,
        zip_safe=False,
        install_requires=[
            'flask',
        ],
    )
    

    MANIFEST.in:

    include MyFlaskProject/myData.sql
    graft MyFlaskProject/static
    graft MyFlaskProject/templates
    global-exclude *.pyc
    
  • 执行python setup.py bdist_wheel命令进行打包项目, 打包完成后,会在根目录下生成文件 /dist/MyFlaskProject-1.0.0-py3-none-any.whl, 该文件就是项目打包后的压缩文件

  • 将该文件MyFlaskProject-1.0.0-py3-none-any.whl发送到其他有python开发环境的电脑上, 直接进行pip install MyFlaskProject-1.0.0-py3-none-any.whl 就可以安装使用了

十二、部署项目

在本地开发的时候可以使用内置的web服务器,但是到真正生产环境的时候不可以这么干,需要使用专业的服务器:如waitress

具体使用方式请百度

相关指令如:

pip install waitress

waitress-serve --call 'MyFlaskProject:create_app'
Serving on http://0.0.0.0:8080

十三、html代码转义问题

对于一段普通的 html渲染到html模板的时候, 默认会原样输出, 如下

import flask
from flask import Flask, render_template, flash, render_template_string, Markup

app = Flask(__name__)

@app.route("/")
@app.route("/index")
def index():
    """该段html代码会直接输出在html页面上"""
    data = "<a href='http://www.baidu.com'>点击查看详情</a>"
    return render_template("index.html", data=data)

if __name__ == '__main__':
    app.run(debug=True)

html页面如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>index首页面</h1>

{{data}}

</body>
</html>

浏览器输出如下:

在这里插入图片描述

要想在html页面解析这段html,方法有三种:

① python代码中使用Markup()

import flask
from flask import Flask, render_template, flash, render_template_string, Markup

app = Flask(__name__)

@app.route("/")
@app.route("/index")
def index():
    """该段html代码会直接输出在html页面上"""
    data = "<a href='http://www.baidu.com'>点击查看详情</a>"
    return render_template("index.html", data=Markup(data))

if __name__ == '__main__':
    app.run(debug=True)

页面如下:

在这里插入图片描述

② html页面中是有|safe过滤器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>index首页面</h1>

{{data|safe}}

</body>
</html>

页面如下:

在这里插入图片描述

③ html中关闭转义设置

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>index首页面</h1>

{% autoescape false %}

{{data}}

{% endautoescape %}

</body>
</html>

页面如下:

在这里插入图片描述

十四、自定义过滤器

方式一:

使用@app.template_filter(“xxx”)注解来定义

import flask
from flask import Flask, render_template, flash, render_template_string, Markup

app = Flask(__name__)

@app.route("/")
@app.route("/index")
def index():
    data = "<a href='http://www.baidu.com'>点击查看详情</a>"
    return render_template("index.html", data=data, target_str="javaAndPhp")


"""
使用@app.template_filter注解来进行自定义过滤器
"""
@app.template_filter("my_reverse_filter")
def reverse_filter(s):
    return s[::-1]

if __name__ == '__main__':
    app.run(debug=True)

在html中使用该过滤器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>index首页面</h1>

{{target_str|my_reverse_filter}}

</body>
</html>

页面显示:

在这里插入图片描述

方式二:

在app启动前进使用app.jinja_env.filters['xxx1'] = xxx2进行注册

import flask
from flask import Flask, render_template, flash, render_template_string, Markup

app = Flask(__name__)
app.secret_key = "ABC"


@app.route("/")
@app.route("/index")
def index():
    data = "<a href='http://www.baidu.com'>点击查看详情</a>"
    return render_template("index.html", data=data, target_str="javaAndPhp")


def test_filter(s):
    return s[::-1]


if __name__ == '__main__':
	# 注册自定义的过滤器  
    app.jinja_env.filters['my_reverse_filter'] = test_filter
    app.run(debug=True)

在html中使用该过滤器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>index首页面</h1>

{{target_str|my_reverse_filter}}

</body>
</html>

界面显示如下:

在这里插入图片描述

十五、环境处理器

环境处理器有点像过滤器

用法一: 向模板页面传递变量

使用 @app.context_processor注解传递变量; 当访问一个url时, 被该注解标注的方法会自动执行,并将变量传递到html模板页面

from flask import Flask, render_template, flash, render_template_string, Markup

app = Flask(__name__)

@app.route("/")
@app.route("/index")
def index():
    data = "<a href='http://www.baidu.com'>点击查看详情</a>"
    return render_template("index.html")

"""
使用 @app.context_processor 向模板传递变量
"""
@app.context_processor
def my_env_param():
    print("被调用了...")
    return dict(user="ZhangSan and LiSi")


if __name__ == '__main__':
    app.run(debug=True)

html页面如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>index首页面</h1>

{{user}}

</body>
</html>

响应如下:

在这里插入图片描述

用法二: 向html模板页面传递Python函数或者自定义函数

这个用法非常广泛, 可以传递自定义函数来作为过滤器使用

也可以传递python内置函数(因为在模板html页面中直接使用python内置函数会有受限, 很多内置函数没法直接使用)

from flask import Flask, render_template, flash, render_template_string, Markup
from datetime import date

app = Flask(__name__)

@app.route("/")
@app.route("/index")
def index():
    data = "<a href='http://www.baidu.com'>点击查看详情</a>"
    return render_template("index.html")

"""
使用 @app.context_processor 向模板传递python内置函数 datetime.date.today(); 该函数在html模板中不能直接使用,通过
这种方式就可以传递到html模板中进行使用了
"""
@app.context_processor
def my_env_param():
    return dict(my_date=datetime.date.today())


if __name__ == '__main__':
    app.run(debug=True)

html页面内容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>index首页面</h1>

{{my_date}}

</body>
</html>

响应结果如下:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/544870.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SpringBoot开发实用篇1

一、热部署 1.手工启动热部署 经过测试当前是没有启动热部署的&#xff1a; 再将sout的信息多复制几份&#xff0c;刷新功能&#xff0c;发现控制台还是只打印一行信息。说明当前热部署是没有生效的。 手动开启热部署&#xff1a; 在pom.xml文件中加入开发者工具配置热部署…

springBoot-Mybatis-Plus 多数据源切换实现

前言&#xff1a;本文主要通过AbstractRoutingDataSource&#xff0c;实现根据 http 访问携带的标识动态切换数据源&#xff1b; 1 AbstractRoutingDataSource 介绍&#xff1a; AbstractRoutingDataSource 是 Spring 框架中的一个抽象类&#xff0c;它可以用来实现动态数据源…

2023年数据分析的就业薪资水平情况

2023年数据分析的就业薪资水平情况 数据已经成为我们工作生活不可缺少的一部分&#xff0c;也成为企业提高竞争力的有效支撑。随着越来越的企业进行数字化转型&#xff0c;对于数据的需求也将越来越大&#xff0c;那么对于正在学习数据分析或者想学习数据分析的小伙伴来说&…

浅析基于AI视频智能识别技术的医疗废弃物智能监管及风险预警方案

一、方案背景 医疗废弃物含有大量的细菌、病毒、化学污染物等&#xff0c;若是回收处置不当、工作人员防护不到位等&#xff0c;会严重影响公众及个人的健康及周围环境。 对医疗废弃物的规范管理&#xff0c;也成为医疗废弃物处置行业的重要一环。传统视频监控方案主要依靠监…

漏电保护插座插排真的有用吗?同为科技(TOWE)漏保系列PDU产品

所谓漏电保护&#xff0c;是指当电气设备绝缘发生故障&#xff0c;电线和地之间、线路和线路之间、工作回路与不能带电的金属壳体形成电流通路&#xff0c;叫做漏电&#xff1b;为预防漏电对人体造成伤害&#xff0c;就产生了各种漏电保护装置&#xff0c;当电路中的漏电流超过…

数字孪生:双碳目标推动下的汽车动力电池发展

据中汽协统计&#xff0c;2022年我国新能源汽车持续爆发式增长&#xff0c;销量超680万辆&#xff0c;已连续8年位居世界第一&#xff0c;保持“快车道”发展态势&#xff0c;引起西方发达国家的高度重视。相当一部分国家以产品全生命周期碳排放为基础&#xff0c;试图建立新的…

一文带你了解移动入库指南(详细版)

​ 移动入库认证周期&#xff1a; 常规为 4-6 周 中国移动是一家基于 GSM、TD-LTE、FDD-LTE 制式网络的移动通信运营商。日前已建成 5G 基站近 39 万个&#xff0c;并且全面推动 SA 网络&#xff0c;同时和中国广电共同发展 5G 网络。作为全球 5G 网络覆盖广、用户规模大的通信…

Q1业绩整体回暖,影视行业找到增长新路径

凛冬已过&#xff0c;影视行业恢复了生机。 数据显示&#xff0c;今年一季度&#xff0c;影视院线板块全部上市公司分别实现营收、归母净利111.86亿元、10.15亿元&#xff0c;同比增幅为1.44%和53.76%。在经济复苏的背景下&#xff0c;影视行业实现了扭亏为盈和跨越式增长。 …

Fiddler抓包丨最常用功能实战演练

目录 一. 停止抓包 二. 清空会话窗 三. 过滤请求 只显示目标请求 只抓取目标端的请求 四. 解码 五. 设置断点 伪造客户端请求 伪造服务器响应 注意事项 六. 总结 结语 通过上一篇文章Fiddler移动端抓包&#xff0c;我们知道了Fiddler抓包原理以及怎样进行移动端抓包…

虎牙直播在微服务改造的实践总结

博主介绍&#xff1a;✌全网粉丝4W&#xff0c;全栈开发工程师&#xff0c;从事多年软件开发&#xff0c;在大厂呆过。持有软件中级、六级等证书。可提供微服务项目搭建与毕业项目实战、定制、远程&#xff0c;博主也曾写过优秀论文&#xff0c;查重率极低&#xff0c;在这方面…

一文快速了解浏览器Sui Explorer

Sui作为一条基于第一原理重新设计和构建而成的L1公链&#xff0c;所有区块和交易信息皆公开透明&#xff0c;每个人都能自行查看。通过Sui链上浏览器&#xff0c;用户可以迅速了解链上的交易情况&#xff0c;比如当前的TPS和Gas价格&#xff0c;也可以使用Digest来查看特定交易…

工厂安灯呼叫系统解决方案

在选择安灯呼叫系统之前&#xff0c;需要先了解自己的需求。不同的工厂可能有不同的需求&#xff0c;例如生产线的规模、生产过程中可能会出现的问题等。因此&#xff0c;选择安灯呼叫系统之前&#xff0c;需要先考虑自己的需求&#xff0c;以便选择到最适合自己的系统。要从多…

三十九、分布式事务、seata、

1、事务 事务(TRANSACTION)是作为单个逻辑工作单元执行的一系列SQL操作&#xff0c;这些操作作为一个整体一起向系统提交&#xff0c;要么都执行、要么都不执行。 1.1 ACID事务的特点 原子性: 一致性&#xff1a;隔离性持久性 1.2 事务并发带来的问题 脏读 幻读 不可重复读 …

Mongodb Shell 常用操作命令

目录 一、启动与关闭mongodb服务 二、进入shell操作 三、常用shell命令 一、启动与关闭mongodb服务 启动:命令: ./mongod -config ../data/mongodb.conf 关闭命令: ./mongod -config ../data/mongodb.conf -shutdown 二、进入shell操作 命令:./mongo 三、常用shell命令 sh…

mysql查看实时执行的sql

MySQL默认不能实时查看执行的SQL语句&#xff0c;因为这会消耗一定的资源。 要开启这个功能&#xff0c;稍微配置一下&#xff0c;打开这个LOG记录就可以了。 查看开启情况 SHOW VARIABLES LIKE "general_log%";general_log值为OFF说明没有开启&#xff1a; 打开…

C++——内存管理+模块

作者&#xff1a;几冬雪来 时间&#xff1a;2023年5月19日 内容&#xff1a;C——内存管理模块 目录 前言&#xff1a; 1.new和delete操作自定义类型&#xff1a; operator new/delete&#xff1a; 定位new表达式&#xff08;placement-new&#xff09;&#xff1a; …

横向对比 11 种算法,多伦多大学推出机器学习模型,加速长效注射剂新药研发

内容一览&#xff1a;长效注射剂是解决慢性病的有效药物之一&#xff0c;不过&#xff0c;该药物制剂的研发耗时、费力&#xff0c;颇具挑战。对此&#xff0c;多伦多大学研究人员开发了一个基于机器学习的模型&#xff0c;该模型能预测长效注射剂药物释放速率&#xff0c;从而…

软件物料清单:打开软件资产黑匣子的关键钥匙

大家有没有遇到过&#xff0c;手机被免费召回维修的情况&#xff1f; 有些人可能遇到这样的问题&#xff0c;手机购买一段时间后&#xff0c;突然收到手机品牌官方发布的通知&#xff1a;听筒模块上的某个组件可能会发生故障&#xff0c;会出现拨打或接听电话时听筒发不出声音的…

接口自动化【五】(HandleRequests类的封装,及postman上下接口依赖的初步认识)

文章目录 前言一、封装发送请求的操作二、迷惑的知识点三、postman的全局变量机制总结 前言 所有的封装就是一种思想&#xff0c;这种思想能不能想到&#xff0c;其实跟写代码建立思维有很大的关系。 下面也是我学到的一种思想&#xff0c;其中对每个函数有解读。以及易错点的…

算法小课堂(九)分支限界法

一、概述 1.1概念 分支限界法是一种求解最优化问题的算法&#xff0c;常以广度优先或以最小耗费&#xff08;最大效益&#xff09;优先的方式搜索问题的解空间树。其基本思想是把问题的可行解展开&#xff0c;再由各个分支寻找最佳解。 在分支限界法中&#xff0c;分支是使用广…