bottle Python HTTP 查询参数最小示例

为了在 bottle 中获取名为 myparam 的查询参数的值,首先 import request

bottle_query_params.py
from bottle import request

然后使用

bottle_request_params_usage.py
request.params["myparam"]

完整示例:

bottle_query_server.py
#!/usr/bin/env python3
from bottle import route, run, request

@route('/test')
def hello():
    query_param = request.params["myparam"]
    return f"myparam is {query_param}"

run(host='localhost', port=8080, debug=True)

运行此脚本并在浏览器中打开 http://localhost:8080/test?myparam=xyz - 你将看到

output.txt
myparam is xyz

Check out similar posts by category: Python