Python自动化测试之request库详解(一)

在做接口测试,接口自动化测试的时候都会用到很多工具,如postman、jmeter、pytest等工具,除了这些工具外,我们也会用python的第3方库requests来做接口测试。接下来我会专门讲request的系列专题,希望大家能学好request库,为将来做接口自动化测试打好基础。

request简介

requests是python实现的简单易用的HTTP库,属于python的第3方库,通过pip进行安装使用。

requests中文文档:https://2.python-requests.org//zh_CN/latest/user/quickstart.html#

requests安装

1.打开cmd

2.通过pip进行安装

复制代码
# 安装requests
pip install requests

同时,我也准备了一份软件测试视频教程(含接口、自动化、性能等),需要的可以直接在下方观看 ,或者直接关注VX公众号:互联网杂货铺,免费领取

软件测试视频教程观看处:

2023完整版阿里大牛7天软件测试零基础速成内部教程,从基础到项目实战学完即入职。

requests发送get请求

源码:

复制代码
def get(url, params=None, **kwargs):
    r"""Sends a GET request.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the body of the :class:`Request`.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """
 
    kwargs.setdefault('allow_redirects', True)
    return request('get', url, params=params, **kwargs)

1.首先导入requests模块

2.选择get方法请求地址:https://www.cnblogs.com/qican/

3.可以查看请求的返回内容

复制代码
# coding:utf-8
# 导入模块
import requests
# 请求地址
url = 'https://www.cnblogs.com/qican/' 
r = requests.get(url) # 请求返回内容 text = r.text print(text)

4.请求携带参数params

5.请求地址:http://httpbin.org/get?

6.请求参数书写以字典形式编写如{ "name": "requests" }

复制代码
# coding:utf-8
# 导入requests模块
import requests
# 携带参数
params = {
   "name": "request",
    "name1":"python"
}
# 请求地址
url = 'http://httpbin.org/get?'
r = requests.get(url,params=params)
text = r.text
print(text)
 
 
代码结果:
{
  "args": {
    "name": "requests", 
    "name1": "python"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.21.0"
  }, 
  "origin": "116.247.112.151, 116.247.112.151", 
  "url": "https://httpbin.org/get?name=requests&name1=python"
}

通过观察,可以发现最后的url地址已经被更改成了name=requests和name1=python

requests请求post

源码:

复制代码
def post(url, data=None, json=None, **kwargs):
    r"""Sends a POST request.
    :param url: URL for the new :class:`Request` object.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.
    :param json: (optional) json data to send in the body of the :class:`Request`.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """
 
    return request('post', url, data=data, json=json, **kwargs)

1.导入requests模块

2.选择post方法请求:http://apis.juhe.cn/simpleWeather/query

3.输入参数格式{"name":"value"}

复制代码
# coding:utf-8
import requests # 导入模块
url = 'http://apis.juhe.cn/simpleWeather/query'      # 请求地址
# 请求参数
data = {
    "city":"上海",
    "key":"331eab8f3481f37868378fcdc76cb7cd"
}
r = requests.post(data=data,url=url)
print(r.text)

返回值其他内容

复制代码
r.text # 返回全部内容
r.url  # 返回的url地址
r.content  # 返回解码后的内容
r.cookies  # 返回cookies
r.headers   # 返回携带的请求头
r.status_code  # 返回状态码
r.json() # 返回json格式

写在最后

PS:这套软件测试的自学教程合集。 对于在测试行业发展的小伙伴们来说应该会很有帮助。全套内容已经打包到网盘,内容总量接近500个G 。如需要软件测试学习资料,关注公众号(互联网杂货铺),后台回复1,整理不易,给个关注点个赞吧,谢谢各位大佬!

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。

相关推荐
长沙三为智能科技16 小时前
中小企业数据驱动决策:从老板拍脑袋到数据说话的4步实操
python
计算机源码社16 小时前
基于大数据技术的台北市住宅价格影响因素挖掘与可视化分析-基于Python与Hadoop的台北市住宅价格数据仓库构建与可视化
大数据·hadoop·python·数据分析·spark·毕业设计·数据可视化
唐璜Taro16 小时前
Agent Harness 系列 · 第 2篇|同一个问题三种回答
人工智能·python
傻啦嘿哟16 小时前
QQ音乐爬虫:爬取歌单与歌曲信息,做个性化推荐系统
python
计算机源码社16 小时前
基于Hadoop+Spark的乳腺癌病理数据可视化分析系统 基于K-Means聚类与PCA降维的乳腺癌形态特征分析系统
大数据·hadoop·python·数据分析·spark·毕业设计·数据可视化
June bug16 小时前
【HCIA- AI(正课)】4.1 华为智算方案
职场和发展
磁场转动100万匹17 小时前
基于 dlib 与 OpenCV 的疲劳驾驶检测:眼睛纵横比(EAR)原理与代码逐段解析
pytorch·python
我不会起名字32217 小时前
一天一道力扣Hot100(37):深度优先算法--括号生成
java·数据结构·c++·后端·python·算法·go
小白勇闯网安圈17 小时前
第5章 流式输出与人工审核
python·langchain
打工仔折腾 AI17 小时前
数据库上 K8s 之后谁来管?拆解金仓 KES-Operator 的声明式运维方案
人工智能·后端·python·性能优化·ai agent 实战