接口测试——python接口开发(二)
1. python接口开发框架Flask简介与安装
- Flask接口测试框架的简介与安装
- Flask是轻量级的web开发框架
- 相比于其他框架,Flask更自由,灵活
- 相比于其他框架,Flask更容易上手
Flask安装网址:https://flask.net.cn/
- 安装Flask:pip install Flask
2. 使用Flask开发一个Get接口
- 开发get接口
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后,再打开原有的网址后添加/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/<username>")
def hello_world3(username):
print(username)
return "hello22222" + username
if __name__ == "__main__":
app.run()
-
并在python里打印出了aaa
-
路径不能一样,否则打印结果一样
-
以下代码修改路径之后再重启
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/<username>")
def hello_world3(username):
print(username)
return "hello22222" + username
@app.route("/hello/args/<username>")
def hello_world4(username):
print(username)
key = request.args.get("key")
print(key)
return "hello22222" + username + "::::" + key
if __name__ == "__main__":
app.run()
- 并在python里打印出了aaa和qqqqqq
- 如果想在网址后方添加value以下代码
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/<username>")
def hello_world3(username):
print(username)
return "hello22222" + username
@app.route("/hello/args/<username>")
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()
- 重启之后打开http://127.0.0.1:5000/hello/args/aaa?key=qqqqqq&value=vvvvv
- 并在python里打印出了aaa和qqqqqq和vvvvv
3. 使用Flask开发一个Post接口
- 端口号可以在这里修改
- 开发post接口
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)
- 如果直接打开这个链接是无法访问的http://127.0.0.1:5555/mypost
- 修改后的代码:
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)
4. Flask结合PyMySQL接口与数据库的交互
https://pymysql.readthedocs.io/en/latest/user/installation.html
-
这时我们需要打开python下方Terminal
-
输入exit()回车后弹出输入页面
-
输入:pip install PyMySQL
-
mysql的基本连接信息
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()
- 刷新http://127.0.0.1:5000 后查询到的四条信息v: