当前位置: 首页 > 工具软件 > pytest-flask > 使用案例 >

python Flask reqest 测试

相洛华
2023-12-01

server.py

# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: JHC 
@license: None
@contact: JHC000abc@gmail.com
@file: server.py
@time: 2022/09/11/ 21:51
@desc:
"""
import json
from flask import Flask, request

app = Flask(__name__)

@app.route("/")
def test():
    return "Hello world !"

@app.route("/say/<name>")
def say(name):
    return f"say{name}"


@app.route("/post",methods=["POST"])
def post_test():
    data_json = json.loads(request.data)
    username = data_json["username"]
    passwd = data_json["passwd"]
    date = data_json["date"]
    secret = data_json["secret"]
    code_lis = data_json["secret"]["Authentication authorization code"]
    print(username,passwd)
    print(date)
    print(secret)
    print(code_lis)
    return secret

if __name__ == '__main__':
    app.run(debug=True)

requests_test.py

# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: JHC 
@license: None
@contact: JHC000abc@gmail.com
@file: req_test.py
@time: 2022/09/11/ 22:23
@desc:
"""
import datetime
import requests


url = "http://127.0.0.1:5000/post"
data = {
    "username": "user01",
    "passwd":"123456",
    "date":datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
    "secret": {
        "Create time":"2022-09-11 22:47:53",
        "Authorized unit":"bj powernode",
        "Expiration":{
            "Year":2023,
            "Month":12,
            "Day":22,
            "Hours":12,
            "Minutes":12,
            "Seconds":30
        },
        "Authentication authorization code":["4a8046ca-cdce-4325-9016-7b0fa0d967ed","fda22554-1278-3f4e-b7b4-6f8f78bac9ef"]
    }
}

res = requests.post(url,json=data)
print(res.text)
 类似资料: