目录
1. python接口开发框架Flask简介与安装
2. 使用Flask开发一个Get接口
3. 使用Flask开发一个Post接口
4. Flask结合PyMySQL接口与数据库的交互
Flask安装网址:Flask 中文网
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "hello world qqqqqq"
if __name__ == "__main__":
app.run()
http://127.0.0.1:5000/hello
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "hello world qqqqqq"
@app.route("/hello")
def hello_world2():
return "hello22222"
@app.route("/hello/")
def hello_world3(username):
print(username)
return "hello22222" + username
if __name__ == "__main__":
app.run()
from flask import Flask,request
app = Flask(__name__)
@app.route("/")
def hello_world():
return "hello world qqqqqq"
@app.route("/hello")
def hello_world2():
return "hello22222"
@app.route("/hello/")
def hello_world3(username):
print(username)
return "hello22222" + username
@app.route("/hello/args/")
def hello_world4(username):
print(username)
key = request.args.get("key")
print(key)
return "hello22222" + username + "::::" + key
if __name__ == "__main__":
app.run()
from flask import Flask,request
app = Flask(__name__)
@app.route("/")
def hello_world():
return "hello world qqqqqq"
@app.route("/hello")
def hello_world2():
return "hello22222"
@app.route("/hello/")
def hello_world3(username):
print(username)
return "hello22222" + username
@app.route("/hello/args/")
def hello_world4(username):
print(username)
key = request.args.get("key")
value = request.args.get("value")
print(key)
return "hello22222" + username + "::::" + key + "::::" + value
if __name__ == "__main__":
app.run()
from flask import Flask,request
app = Flask(__name__)
@app.route("/mypost",methods=["post"])
def my_post():
return "post request"
if __name__ == "__main__":
app.run(port=5555)
from flask import Flask,request
app = Flask(__name__)
@app.route("/mypost",methods=["post"])
def my_post():
return "post request"
@app.route("/mypost1",methods=["post"])
def my_post1():
# 表单数据类型
username =request.form["username"]
sex = request.form["sex"]
print(username)
print(sex)
return "post request" + username + "::" + sex
@app.route("/mypost1",methods=["post"])
def my_post2():
# json数据类型
request_data =request.get_json()
sex = request.form["sex"]
print(request_data)
print(request_data["user"])
return "post request" + str(request_data)
if __name__ == "__main__":
app.run(port=5555)
Installation — PyMySQL 0.7.2 documentation
import pymysql
from flask import Flask
# 这里是mysql的基本连接信息
conn = pymysql.connect(
host="192.168.1.116",
user="root",
password="123",
database="nydb1",
charset="utf8"
)
cursor = conn.cursor()
app = Flask(__name__)
@app.route("/")
def hello_world():
sel = "select * from school"
result = cursor.execute(sql)
# 得到查询后的真正的结果
r = cursor.fetchall()
print(r)
return str(r)
if __name__ == "__main__":
app.run()
以下是我收集到的比较好的学习教程资源,虽然不是什么很值钱的东西,如果你刚好需要,可以评论区,留言【777】直接拿走就好了
各位想获取资料的朋友请点赞 + 评论 + 收藏,三连!
三连之后我会在评论区挨个私信发给你们~