Python接口测试环境搭建过程详解

Python接口测试环境搭建过程详解,第1张

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 下载apk文件

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3216547.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-03
下一篇 2022-10-03

发表评论

登录后才能评论

评论列表(0条)

保存