欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python post请求的接口测试示例

程序员文章站 2022-05-05 09:54:55
1、client端代码如下# -*- coding:utf-8 -*-import requestsimport pprintrequest_info = { "model": "", "data": { "A": "HAHAH", "B": 10, "C": "BBBBBB", } }resp = requests.post("http://192.168.aa...

1、client端代码如下

# -*- coding:utf-8 -*-
import requests
import pprint

request_info = {
        "model": "",
        "data": {
            "A": "HAHAH",
            "B": 10,
            "C": "BBBBBB",
        }
    }

resp = requests.post("http://192.168.aa.bb:8556/search", json=request_info)
pprint.pprint(resp.status_code)
pprint.pprint(resp.content)

2、server 端代码如下:

# _*_coding:-utf-8_*_
import json
import argparse
import logging
from flask import Flask, request
import SearchProcess      # SearchProcess是自定义的类别

app = Flask(__name__)
SEARCH_PROCESS = SearchProcess()


# 接口测试
@app.route("/search", methods=["POST"])
def test():
    # 获取传入的请求数据
    request_info = json.loads(request.get_data(as_text=True))

    # 得到请求的数据,可以调用相应的函数
    return SEARCH_PROCESS.process(request_info)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    argparse.ArgumentParser(description="test_interface")
    parser.add_argument("-a", "--address", default="192.168.aa.bb")
    parser.add_argument("-p", "--port", type=int, default=8556)
    parser.add_argument('-d', '--debug', action='store_true',
                        help=('Run the application in debug mode (reloads when '
                              'the source code changes and reveals more detailed '
                              'error messages)')
                        )
    args = parser.parse_args()
    logging.info('starting ')
    app.run(host=args.address,
            port=int(args.port), threaded=False,
            debug=args.debug)

 

本文地址:https://blog.csdn.net/u011412768/article/details/107326180