1、安装flask框架。
pip install flask -i https://pypi.tuna.tsinghua.edu.cn/simple/
2、web 示例。
from flask import Flask app = Flask(__name__) @app.route('/') def hello_flask(): return 'Hello, World!' if __name__ == '__main__': app.run(host='0.0.0.0', port=80, debug=False)
3、访问:http://192.168.2.16/
更新【2022-12-07】:
1、get请求、post请求
from flask import Flask from flask import request app = Flask(__name__) @app.route('/') def hello_flask(): return 'Hello, World!' @app.route('/login', methods=['GET']) def hello_world(): # get请求及参数获取方式 # 获取参数对应值 name = request.args.get("name") return "welcome" + " " + name @app.route('/check', methods=['POST']) def hello_check(): # post请求及参数获取方式 # 获取参数对应值 name = request.form.get("name") return "welcome" + " " + name if __name__ == '__main__': app.run(host='0.0.0.0', port=80, debug=False)
通过postman进行get测试
通过postman进行post测试
2、json递交、返回json。
from flask import Flask from flask import request from flask import jsonify app = Flask(__name__) @app.route('/') def hello_flask(): return 'Hello, World!' @app.route('/login', methods=['GET']) def hello_world(): # get请求及参数获取方式 # 获取参数对应值 name = request.args.get("name") return "welcome" + " " + name @app.route('/check', methods=['POST']) def hello_check(): # post请求及参数获取方式 # 获取参数对应值 name = request.form.get("name") return "welcome" + " " + name @app.route('/sync', methods=['POST']) def hello_sync(): # post请求及参数获取方式 # 获取参数对应值 data = request.get_json() name = data['name'] code = data['code'] # return "welcome" + " " + name + " " + code return jsonify({"login": name, "token": 9}) if __name__ == '__main__': app.run(host='0.0.0.0', port=80, debug=False)