使用python的接口测试环境搭建及使用
Posted 拉努斯石
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python的接口测试环境搭建及使用相关的知识,希望对你有一定的参考价值。
环境搭建
python 安装:建议使用python3.7
pycharm安装
requests安装 :pip3 install requests
requests 基本使用
usage:
>>> import requests
>>> r = requests.get(‘https://www.python.org‘)
>>> r.status_code
200
>>> ‘Python is a programming language‘ in r.content
True
... or POST:
>>> payload = dict(key1=‘value1‘, key2=‘value2‘)
>>> r = requests.post(‘https://httpbin.org/post‘, data=payload)
>>> print(r.text)
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
获取返回数据:
res = requests.post(url,data).text 返回的是text格式
res = requests.post(url,data).json 返回的是json格式
requests上传文件的例子:
import requests
import json
#上传文件
#url = ‘https://www.imooc.com/user/postpic‘
download_url = ‘http://file.mukewang.com/imoocweb/webroot/mobile/imooc7.2.010102001android.apk‘
file = {
"fileField":("test.jpg",open("E:/ytxu/test.jpg","rb"),"image/jpg"),
"type":"1"
}
cookie = {
"apsid":"I5ZTVmZmUzMGE1NDY2OTljZjFjYzkyMTMyMjk3MmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANzIxMzU2MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABNdXNoaXNoaV94dUAxNjMuY29tAAAAAAAAAAAAAAAAADVjZDY5ZWYxMGQ2MmFlZDVmNTJkYWQ0ZWNhNjU5MjZhz%2BMFXc%2FjBV0%3DZW"
}
res = requests.get(download_url)
with open("mukewang.apk","wb") as f:
f.write(res.content)
#res = requests.post(url,files=file,cookies=cookie,verify=False).json()
print(res)
#res = requests.post(url,files=file,cookies=cookie,verify=False).json()
print(res)
requests 中 hearder介绍
requests hearder加密
flask环境搭建及简单开发
接口开发:
#coding=utf-8
import requests
import json
from flask import Flask
app = Flask(__name__)
@app.route("/")
def logine():
data = json.dump(
{
‘username‘: "aa",
‘password‘: "111111"
}
)
return data
if __name__ == "__main__":
app.run()
以上是关于使用python的接口测试环境搭建及使用的主要内容,如果未能解决你的问题,请参考以下文章