返回顶部
扶摇直上九万里,展翅高飞岂可待。

接口测试——python接口开发(二)

1. python接口开发框架Flask简介与安装

1675425942349.png

  • Flask接口测试框架的简介与安装
  • Flask是轻量级的web开发框架
  • 相比于其他框架,Flask更自由,灵活
  • 相比于其他框架,Flask更容易上手

Flask安装网址:https://flask.net.cn/

  • 安装Flask:pip install Flask

1675495632789.png

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()

1675499464693.png

  • 如果想连续网址后面的参数,那就在写代码时添加/hello后,再打开原有的网址后添加/hello,再重启
    1675498960134.png

http://127.0.0.1:5000/hello

1675499429739.png

  • 如果只是在网址后随意输入任意参数结果是无法接收到的

1675499630762.png

  • 以下解决方法:
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()

1675499945338.png

  • 并在python里打印出了aaa
    1675499989614.png

  • 路径不能一样,否则打印结果一样
    1675500733859.png

  • 以下代码修改路径之后再重启

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()

1675501012815.png

  • 并在python里打印出了aaa和qqqqqq
    1675500956879.png
  • 如果想在网址后方添加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

1675501783568.png

  • 并在python里打印出了aaa和qqqqqq和vvvvv
    1675501819303.png

3. 使用Flask开发一个Post接口

  • 端口号可以在这里修改
    1675502152888.png
  • 开发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

1675502491645.png

  • 修改后的代码:
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

1675503235108.png

  • 这时我们需要打开python下方Terminal
    1675503277359.png

  • 输入exit()回车后弹出输入页面

  • 输入:pip install PyMySQL
    1675503390081.png

  • 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:

1675504774216.png

1675504609850.png

posted @ 2023-03-20 10:44  孙凯玉  阅读(54)  评论(0编辑  收藏  举报