5 接口测试梳理
1 总结
2 server
from flask import Flask, render_template from flask import request import json import time # 实例化一个 web 服务对象 app = Flask(__name__) # 定义 json 返回模板 resp_data = { 'code': 1, 'data': [], 'msg': '请求成功!' } # 接收 get 请求,返回 json @app.route(methods=['GET'], rule='/get/one') def get_one(): req_data = request.args # 获取 url 中的参数 username = req_data['username'] password = req_data['password'] if(username != None and password != None): user = { "username": username, "password": password } resp_data['data'].clear() resp_data['data'].append(user) return json.dumps(resp_data) else: resp_data['msg'] = '用户名或密码为空!' return json.dumps(resp_data) # 接收 get 请求,返回 json @app.route(methods=['GET'], rule='/get/two') def get_two(): req_data = request.args username = req_data['username'] password = req_data['password'] if(username != None and password != None): user = { "username": username, "password": password } resp_data['data'].clear() resp_data['data'].append(user) return json.dumps(resp_data) else: resp_data['msg'] = '用户名或密码为空!' return json.dumps(resp_data) # 接收 form 请求,返回 json @app.route(methods=['POST'], rule='/post/one') def post_one(): req_data = request.form # 获取表单数据 # print(type(req_data)) # print(req_data.getlist('username')) username = req_data.get('username') password = req_data.get('password') print(username) print(password) if (username != None and password != None): user_post_one = { "username": username, "password": password } resp_data['data'].clear() resp_data['data'].append(user_post_one) return json.dumps(resp_data) else: resp_data['msg'] = '用户名或密码为空!' return json.dumps(resp_data)
# 接收 json 请求,返回 json @app.route(methods=['POST'], rule='/post/two') def post_two(): req_data = json.loads(request.get_data()) # 获取 json 数据 username = req_data.get('username') password = req_data.get('password') if (username != None and password != None): user_post_two = { "username": username, "password": password } resp_data['data'].clear() resp_data['data'].append(user_post_two) return json.dumps(resp_data) else: resp_data['msg'] = '用户名或密码为空!' return json.dumps(resp_data)
# 接收 upload file,返回 json @app.route(methods=['POST'], rule='/uploadfile') def down_file(): file_obj = request.files['pic'] # 获取上传的文件 if (file_obj is None): # 表示没有发送文件 resp_data['data'].clear() resp_data['msg'] = "未上传文件!" return json.dumps(resp_data) else: filename = file_obj.filename file_obj.save(filename) # 保存文件 # return "文件接收成功!"; resp_data['data'].clear() resp_data['data'].append(filename) resp_data['msg'] = u'文件上传成功!' return json.dumps(resp_data) if __name__ == '__main__': # 运行服务,并指定 ip 和 端口号。右键运行即可 app.run(host='127.0.0.1', port='9090')
3 client
import requests # 初始化 session = requests.session() host = 'http://127.0.0.1:9090' # requests 模块发送请求有 data、json、params 三种携带参数的方法 # params 在 get 请求中使用 # data、json 在 post 请求中使用 # 发送第一种类型的 get 请求,参数在 url 后面 # http://127.0.0.1:9090/?username=lizi&password=123456 def get_one(): path = '/get/one' url = host + path + '?username=lizi_get_one&password=123456' headers = {} params = {} resp = session.get(url=url, headers=headers, params=params) if(resp.status_code == 200): print(resp.json()) else: print(resp.status_code) print(resp.text) # 发送第二种类型的 get 请求,参数在 params 变量中 def get_two(): path = '/get/two' url = host + path headers = {} params = { "username":"lizi_get_two", "password":"123456" } resp = session.get(url=url, headers=headers, params=params) if(resp.status_code == 200): print(resp.json()) else: print(resp.status_code) print(resp.text) # 发送第一种类型的 post 请求 # Content-Type=application/x-www-form-urlencoded # 发送 get 请求时,客户端把 form 数据转换成一个字串 append 到 url 后面,用 ? 分割。(可省略) # 发送 post 请求时,客户端把 form 数据封装到 http body中,然后发送到 server def post_one(): path = '/post/one' url = host + path headers = { "Content-Type":"application/x-www-form-urlencoded" # 请求体为 form } data = { "username":"lizi_post_one", "password":"123456" } resp = session.post(url=url, headers=headers, data=data) if(resp.status_code == 200): print(resp.json()) else: print(resp.status_code) print(resp.text) # 发送第二种类型的 post 请求 # Content-Type=application/json # 发送 post 请求时,客户端把 json 数据封装到 http body中,然后发送到 server def post_tow(): path = '/post/two' url = host + path headers = { "Content-Type":"application/json" # 请求体为 json } json = { "username":"lizi_post_two", "password":"123456" } resp = session.post(url=url, headers=headers, json=json) if(resp.status_code == 200): print(resp.json()) else: print(resp.status_code) print(resp.text) # 发送第三种类型的 post 请求 # Content-Type=multipart/form-data # 发送 post 请求时,一般多用于文件上传 def upload_file(file_name): path = '/uploadfile' url = host + path files = { 'pic': open(file_name, 'rb') # pic 供 server 用 } resp = requests.post(url=url, files=files) if(resp.status_code == 200): print(resp.json()) else: print(resp.status_code) print(resp.text) if __name__ == "__main__": get_one() get_two() post_one() post_tow() upload_file('E:\\lizi.jpg')