路由模糊匹配

cooolr 于 2020-11-25 发布
from flask import Flask
from werkzeug.routing import BaseConverter

class RegexConverter(BaseConverter):
    def __init__(self, map, *args):
        self.map = map
        self.regex = args[0]

app = Flask(__name__)
app.url_map.converters['regex'] = RegexConverter

# 匹配/kill/后任意字符
@app.route('/kill/<regex(".*"):name>')
def killname(name):
    return "ps -ef | grep '%s' | grep -v grep | awk '{print $2}' | xargs kill -9"%name

app.run()