from flask import Blueprint, jsonify, request, redirect, url_for from flask import render_template antispider = Blueprint('antispider', __name__, url_prefix='/as') @antispider.before_request def before_request(): user_agent = request.headers.get("User-Agent") print(user_agent) if "Mozilla" in user_agent: # 允许请求通过 return None else: return "错误请求", 403 @antispider.route('/show') def index(): return render_template("antispider/ua_show.html") @antispider.route('/error403') def error403(): return "缺少关键参数", 403 @antispider.route('/cookie_demo') def cookie_demo(): if 'story' in request.cookies: # 如果存在 'story' cookie,则执行相应操作 return render_template("antispider/cookie_demo.html") else: return "没有权限", 403