flask框架搭建网站1

1.快速搭建网站

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return "this is myweb"

@app.route('/index')
def index():
    return "this is myweb index"

if __name__=='__main__': 
    app.run(host="127.0.0.1", port=5000)

  

 

 

访问:

http://127.0.0.1:5000

http://127.0.0.1:5000/index

 

2.带get参数的网站

from flask import Flask,request

app = Flask(__name__)

@app.route('/')
def hello_world():
    return "this is myweb"

@app.route('/index')
def index():
    age = request.args.get("age")
    pwd = request.args.get("pwd")
    print(age, pwd)
    return "this is myweb index"

if __name__=='__main__': 
    app.run(host="127.0.0.1", port=5000)

  

访问:

http://127.0.0.1:5000/index?age=1&pwd=123

 

3.post请求(请求体内有参数)

from flask import Flask,request

app = Flask(__name__)

@app.route('/')
def hello_world():
    return "this is myweb"

@app.route('/index', methods=["POST"])
def index():
    age = request.from.get("age")
    pwd = request.from.get("pwd")
    print(age, pwd)
    return "this is myweb index"

if __name__=='__main__': 
    app.run(host="127.0.0.1", port=5000)

  

访问:(请求体里有 age=1 pwd=123)

http://127.0.0.1:5000/index  

 

4.post请求(请求体内有json格式参数)

from flask import Flask,request

app = Flask(__name__)

@app.route('/')
def hello_world():
    return "this is myweb"

@app.route('/index', methods=["POST"])
def index():
    data = request.json
    print(data)
    return "this is myweb index"

if __name__=='__main__': 
    app.run(host="127.0.0.1", port=5000)

  

 

5.返回json格式内容

 

from flask import Flask,request,jsonify

app = Flask(__name__)

@app.route('/')
def hello_world():
    return "this is myweb"

@app.route('/index')
def index():
    return jsonify({age:1, pwd:123})

if __name__=='__main__': 
    app.run(host="127.0.0.1", port=5000)

  

posted @ 2025-07-02 23:26  铿锵有力自信且坚定  阅读(11)  评论(0)    收藏  举报