ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
对于一个web网站来说,往往都是接收用户的输入然后输出相应的数据。而接收的输入主要有get参数,和post参数。 * get参数:在url上可以看到的 * post参数:在body里面的,只能通过工具查看 bottle处理get请求需要引入request对象,然后通过request.query这个字典对象(FormsDict)可以拿到get参数 ~~~ # coding:UTF-8 from bottle import Bottle, request app = Bottle() @app.get('/') def index(): return request.query.get('a', 'default') app.run(host="127.0.0.1", port=8000, reloader=True, debug=True) ~~~ bottle处理post请求需要引入request对象,然后通过request.forms这个字典对象(FormsDict)可以拿到post参数 ~~~ # coding:UTF-8 from bottle import Bottle, request app = Bottle() @app.get('/') def index(): return request.forms.get('a', 'default') app.run(host="127.0.0.1", port=8000, reloader=True, debug=True) ~~~ **一些说明:** 1. bottle拿到的参数是str对象,所以自己要注意类型转换 2. 字典对象的get方法可以在key不存在的时候使用默认值这个特性要好好利用 3. request对象封装好了对输入参数的处理,要好好使用