Python接口自动化测试之Requests库&Pytest框架

前言:

感谢各位读者阅读七叔的文章,感谢每一位读者,如果方便的话麻烦动一下发财的小手点个关注,七叔写的文章可能不是那么生动有趣但是还是希望能给各位带来提升,,文章最后有叔的一些干货也是七叔十三年测试所保留下来的资源,各位如果感兴趣的话可以来看看

  1. 发送get请求

#导包
import requests
#定义一个url
url = "http://xxxxxxx"
#传递参数
payload="{\"head\":{\"accessToken\":\"\",\"lastnotice\":0,\"msgid\":\"\"},\"body\":{\"user_name\":\"super_admin\",\"password\":\"b50c34503a97e7d0d44c38f72d2e91ad\",\"role_type\":1}}"
headers = {
  'Content-Type': 'text/plain',
  'Cookie': 'akpsysessionid=bafc0ad457d5a99f3a4e53a1d4b32519'
}
#发送get请求
r = requests.get( url=url,headers=headers, data=payload)
#打印结果
print(r.text)
#解码
print(r.encoding)
print(r.text.encode('utf-8').decode('unicode_escape'))#先把返回的结果转换成utf-8,再去解码成中文的编码
  1. 发送post请求

#导包
import requests
#定义一个url
url = "http://xxxxxxx"
#传递参数
payload="{\"head\":{\"accessToken\":\"\",\"lastnotice\":0,\"msgid\":\"\"},\"body\":{\"user_name\":\"super_admin\",\"password\":\"b50c34503a97e7d0d44c38f72d2e91ad\",\"role_type\":1}}"
headers = {
 'Content-Type': 'text/plain',
 'Cookie': 'akpsysessionid=bafc0ad457d5a99f3a4e53a1d4b32519'
}
#发送post请求
r = requests.post( url=url,headers=headers, data=payload)
#打印结果
print(r.text)
  1. 发送https请求

import requests
url='https://www.ctrip.com/'
#第一种解决方案,发送请求的时候忽略证书,证书的参数verify用的比较多
r=requests.post(url=url,verify=False)#verify参数默认为True,值为False,表示忽略证书
#第二张解决方案,verify里面添加证书的路径
r=requests.post(url=url,verify='证书的路径')#verify参数默认为True,值为False,表示忽略证书
print(r.text)
  1. 文件上传

import requests
file = {
    'filename':open('文件名称','rb')
}
response = requests.post("网址",file)
print(response.text)
  1. 文件下载

#小文件下载
import requests
r = requests.get("https://img.sitven.cn/Tencent_blog_detail.jpg")
with open(r"D:\a.jpg", "wb") as f:
    f.write(r.content)

#大文件下载
import requests
def test_downloads(url, file):
    s = requests.session()
    r = s.get(url, stream=True, verify=False)
    with open(file, "wb") as f:
        for chunk in r.iter_content(chunk_size=512):
            f.write(chunk) 
if __name__ == "__main__":
    url = "https://www.url.com/test/export"
    file = "D:\\a.xlsx"
    test_downloads(url=url, file=file)
#转载至:https://blog.csdn.net/weixin_43507959/article/details/107326912
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值