requests入门教程有哪些?

2026-06-10 12:50:51 577阅读 0评论 SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计2939个文字,预计阅读时间需要12分钟。

02 使用requests库进行接口测试 - requests库的安装+安装常见问题+提示连接不上,443问题+通常是因为浏览器设置了代理,关闭代理。+网络加载慢,设置国内镜像地+址+1. 使用pip安装+2. 安装PyCharm+3. 设置国内镜像源+清华

requests入门教程有哪些?

02 requests接口测试-requests的安装

安装常见问题

  • 提示连接不上,443问题

一般是因为浏览器设置了代理,关闭代理。

requests入门教程有哪些?

  • 网络加载慢,设置国内镜像地址

1.pip安装

2.pycharm安装

  • 国内镜像源

清华:​​pypi.tuna.tsinghua.edu.cn/simple​​

阿里云:​​mirrors.aliyun.com/pypi/simple/​​

中国科技大学 ​​pypi.mirrors.ustc.edu.cn/simple/​​

华中理工大学:​​pypi.hustunique.com/​​

山东理工大学:​​pypi.sdutlinux.org/​​

豆瓣:​​pypi.douban.com/simple/​​

  • 临时使用

在使用pip的时候加参数-i ​​pypi.tuna.tsinghua.edu.cn/simple​​

例如:pip install -i ​​pypi.tuna.tsinghua.edu.cn/simple​​ pyspider,这样就会从清华镜像去安装pyspider库。

03 requests接口测试-requests原理与代码解析

  • requests工作原理
  • 代码解析

通过追踪代码,可以发现requests实际上是调用了request方法,源码如下:

method, url是必填参数,其他是可选参数。

参数method,有​​GET​​, ​​OPTIONS​​, ​​HEAD​​, ​​POST​​, ​​PUT​​, ​​PATCH​​, ​​DELETE​​。

可选参数可以发送data、json、headers、cookies、flies、auth、timeout、proxies、verify、stream、cert

def request(method, url, **kwargs):
"""Constructs and sends a :class:`Request <Request>`.

:param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How many seconds to wait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
:return: :class:`Response <Response>` object
:rtype: requests.Response

Usage::

>>> import requests
>>> req = requests.request('GET', 'tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860​​

参数:

tel:手机号码

返回:JSON

import requests
def test_get():
# 参数保存在url中,?后面添加参数名称和参数值,如果有多个参数,使用&连接
url = 'tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860'
r=requests.get(url=url)
print(r.text)
print(r.status_code)
print(r.cookies)
print(r.url)
# 参数保存在params中,params以字典形式保存数据
url = 'tcc.taobao.com/cc/json/mobile_tel_segment.htm'
params={'tel':13909161860}
r=requests.get(url=url,params=params)
print(r.text)
print(r.status_code)
print(r.cookies)
print(r.url)

获取响应结果

import requests

def test_requests():
r = requests.get('www.baidu.com')
print('\n')
print('状态码是:', r.status_code)
print('url是:', r.url)
print('头信息是:', r.headers)
print('cookies是:', r.cookies)
print('text是:', r.text)
print('请求是:', r.request)

05 requests接口测试-post请求

  • post发送form格式请求参数,使用data
  • post发送json格式请求参数,使用json

06 requests接口测试-cookies值

  • 什么是cookies?

相当于临时身份证。

  • 谷歌浏览器抓包

查看请求参数的格式,看Content-Type字段

如:Content-Type: text/html;charset=utf8,表示请求参数是text格式,编码是utf-8

如果是form式,可以查看form data具体发送的参数

在脚本中发送

查看cookies

  • 解码

编码:把码位转换成字节序列(通俗来说:把字符串转换成用于存储或传输的字节序列,python中是.encode())

解码:把字节序列转换成码位(通俗来说:把字节序列转换成人类可读的文本字符串,python中是.decode())

07 requests接口测试-headers值

headers参数不是每个接口都需要添加的,开发可以自行定义。

  • 不需要添加headers
import requests
def test_51job():
# 前程无忧搜索岗位不需要headers信息
url='search.51job.com/list/030200,000000,0000,00,9,99,%25E8%25BD%25AF%25E4%25BB%25B6%25E6%25B5%258B%25E8%25AF%2595%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare='
r_51job=requests.get(url=url)
# print(r_51job.text)
print(r_51job.encoding) # html默认的编码方式是ISO-8859-1
r_51job.encoding='gb2312' # 修改编码方式
print(r_51job.text)
  • 乱码问题
  • 需要添加headers
import requests
def test_12306():
# 12306搜索车票需要添加headers信息,只需要cookies即可。实际工作中,headers需要哪些值,咨询开发人员了解。
url='kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2020-06-06&leftTicketDTO.from_station=SHH&leftTicketDTO.to_station=GZQ&purpose_codes=ADULT'
headers={'Cookie': 'JSESSIONID=C5DCA622147CEF2C4D9C3D3E53E9CF26; BIGipServerotn=1406730506.64545.0000; RAIL_EXPIRATION=1591740743186; RAIL_DEVICEID=FqtrEO_1lQCMdLmY_0uxjBNyLf5esH-V-KtA_I-kPu0j721_HYTxo4IobbdtZbAr75fLtGhLHAQTE8mWEaNmM7ococf3hUVIFw-Gaaper7CzwlrDFsHwwey8w_YQ5gGoMoyfKgVJ5o4nNuWVYhuiC_cxPzsWFJkF; BIGipServerpassport=954728714.50215.0000; route=6f50b51faa11b987e576cdb301e545c4; _jc_save_toStation=%u5E7F%u5DDE%2CGZQ; _jc_save_fromDate=2020-06-06; _jc_save_toDate=2020-06-06; _jc_save_wfdc_flag=dc; _jc_save_fromStation=%u4E0A%u6D77%2CSHH'}
r_12306=requests.get(url=url,headers=headers)
print(r_12306.text)
  • 使用chrome查看headers

08 requests接口测试-www.ctrip.com/'
r=requests.get(url=url,verify=False)
print(r.text)
  • 在verify里面添加证书路径

09 requests接口测试-上传文件files参数

  • 携程上传头像
import requests

def test_files():
url='sinfo.ctrip.com/MyInfo/Ajax/UploadPhoto.ashx' # 携程上传头像
cookies={'cookie': 'cookies的值'}
files={'uploadFile_852':('Markdown.jpg',open('D:\\Markdown.jpg','rb'),'image/jpeg')}
r=requests.post(url=url,files=files,verify=False,cookies=cookies)
print(r.text)

files字段说明:

uploadFile_852 文件参数的名字,通过抓包工具查看,实际工作中直接向开发咨询了解。

Markdown.jpg 文件名

open('D:\Markdown.jpg','rb') 打开本地一个文件,注意使用\转义

image/jpeg 文件格式

  • 禅道上传csv文件

上传文件,你一般需要什么参数?

1.url

2.请求参数

3.请求方式

4.files参数

5.cookies

  • 接口测试两个检查点

1.校验返回结果与开发定义的(接口文档)是否一致,如上传成功返回1,上传失败返回0

2.校验接口逻辑是否正确,如上传文件后会把文件保存在服务器的一个目录下,到该目录下检查是否存在上传的文件

10 requests接口测试-timeout超时

  • 超时测试介绍
  • 超时异常
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='tcc.taobao.com', port=80): Max retries exceeded with url: /cc/json/mobile_tel_segment.htm?tel=13909161860 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x000001576DCE1148>, 'Connection to tcc.taobao.com timed out. (connect timeout=0.01)'))import requests

def test_timeout():
url = 'tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860'
r=requests.get(url=url,timeout=1)
print(r.text)
  • 轮循
import requests

def test_files():
# 携程上传用户头像
for i in range(0,10):
try:
url='sinfo.ctrip.com/MyInfo/Ajax/UploadPhoto.ashx'
cookies={'cookie': 'cookies的值')}
r=requests.post(url=url,files=files,verify=False,cookies=cookies,timeout=0.25)
print(r.text)
except:
print('运行出错')

11 requests接口测试-auth鉴权

12 requests接口测试-文件下载接口

  • 导出禅道用例模板
  • 思路
  • stream参数的使用

13 requests接口测试-session操作

  • 为什么要使用session参数?

有很多接口需要一些公共的参数,比如cookies,可节省时间,精简代码

def test_seesion():
s=Session()
s.headers={'h':'test_headers'}
r=s.post('pay.weixin.qq.com/wiki/doc/api/index.html​​​

​​pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3​​

  • 微信支付签名算法

(签名校验工具)

签名生成的通用步骤如下:

第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA。

特别注意以下重要规则:

◆ 参数名ASCII码从小到大排序(字典序);

◆ 如果参数的值为空不参与签名;

◆ 参数名区分大小写;

◆ 验证调用返回或微信主动通知签名时,传送的sign参数不参与签名,将生成的签名与该sign值作校验。

◆ 微信接口可能增加字段,验证签名时必须支持增加的扩展字段

第二步,在stringA最后拼接上key得到stringSignTemp字符串,并对stringSignTemp进行MD5运算,再将得到的字符串所有字符转换为大写,得到sign值signValue。

◆ key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置

举例:

假设传送的参数如下:

appid: wxd930ea5d5a258f4f

mch_id: 10000100

device_info: 1000

body: test

nonce_str: ibuaiVcKdpRxkhJA

第一步:对参数按照key=value的格式,并按照参数名ASCII字典序排序如下:

stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA";

第二步:拼接API密钥:

stringSignTemp=stringA+"&key=192006250b4c09247ec02edce69f6a2d" //注:key为商户平台设置的密钥key

sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A9CF3B7" //注:MD5签名方式

sign=hash_hmac("sha256",stringSignTemp,key).toUpperCase()="6A9AE1657590FD6257D693A078E1C3E4BB6BA4DC30B23E0EE2496E54170DACD6" //注:HMAC-SHA256签名方式,部分语言的hmac方法生成结果二进制结果,需要调对应函数转化为十六进制字符串。

最终得到最终发送的数据:

<xml>

<appid>wxd930ea5d5a258f4f</appid>

<mch_id>10000100</mch_id>

<device_info>1000</device_info>

<body>test</body>

<nonce_str>ibuaiVcKdpRxkhJA</nonce_str>

<sign>9A0A8659F005D6984697E2CA0A9CF3B7</sign>

</xml>


简单来说,就是通过上面的规则生成sign签名,然后放在sign参数发送。

proxies代理机制

proxies = {'127.0.0.1:8888', '127.0.0.1:8888'}
def test_proxies():
r = requests.get("www.baidu.com/'
sit_ip='cn.bing.com/'
return sit_ip

def sql_conf():
# 定义数据库的配置
ip=''
username=''
password=''
port=3306
charset='utf-8'
return ip,username,password,port,charsetimport requests
from config.conf import * # 从config导入

def test_conf():
url=server_ip()+'/login' # server_ip()表示引入这个函数,而不是这个方法
print(url)
r=requests.get(url=url)
print(r.text)
import requests
from config.conf import * # 从config导入

def test_conf():
url=server_ip()+'/login' #
print(url)
r=requests.get(url=url)
print(r.text)

19 requests接口自动化-excel参数化

20 requests接口自动化-数据库参数化

21 requests接口自动化-列表与字典参数化

22 requests接口自动化-动态关联text/html格式

23 requests接口自动化-动态关联json格式

24 requests接口自动化-assert断言

25 pytest框架-安装

26 pytest框架-运行测试用例

27 pytest框架-动态关联

28 pytest框架-生成测试报告

29 Jenkins+requests+pytest+allure持续集成




本文共计2939个文字,预计阅读时间需要12分钟。

02 使用requests库进行接口测试 - requests库的安装+安装常见问题+提示连接不上,443问题+通常是因为浏览器设置了代理,关闭代理。+网络加载慢,设置国内镜像地+址+1. 使用pip安装+2. 安装PyCharm+3. 设置国内镜像源+清华

requests入门教程有哪些?

02 requests接口测试-requests的安装

安装常见问题

  • 提示连接不上,443问题

一般是因为浏览器设置了代理,关闭代理。

requests入门教程有哪些?

  • 网络加载慢,设置国内镜像地址

1.pip安装

2.pycharm安装

  • 国内镜像源

清华:​​pypi.tuna.tsinghua.edu.cn/simple​​

阿里云:​​mirrors.aliyun.com/pypi/simple/​​

中国科技大学 ​​pypi.mirrors.ustc.edu.cn/simple/​​

华中理工大学:​​pypi.hustunique.com/​​

山东理工大学:​​pypi.sdutlinux.org/​​

豆瓣:​​pypi.douban.com/simple/​​

  • 临时使用

在使用pip的时候加参数-i ​​pypi.tuna.tsinghua.edu.cn/simple​​

例如:pip install -i ​​pypi.tuna.tsinghua.edu.cn/simple​​ pyspider,这样就会从清华镜像去安装pyspider库。

03 requests接口测试-requests原理与代码解析

  • requests工作原理
  • 代码解析

通过追踪代码,可以发现requests实际上是调用了request方法,源码如下:

method, url是必填参数,其他是可选参数。

参数method,有​​GET​​, ​​OPTIONS​​, ​​HEAD​​, ​​POST​​, ​​PUT​​, ​​PATCH​​, ​​DELETE​​。

可选参数可以发送data、json、headers、cookies、flies、auth、timeout、proxies、verify、stream、cert

def request(method, url, **kwargs):
"""Constructs and sends a :class:`Request <Request>`.

:param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How many seconds to wait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
:return: :class:`Response <Response>` object
:rtype: requests.Response

Usage::

>>> import requests
>>> req = requests.request('GET', 'tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860​​

参数:

tel:手机号码

返回:JSON

import requests
def test_get():
# 参数保存在url中,?后面添加参数名称和参数值,如果有多个参数,使用&连接
url = 'tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860'
r=requests.get(url=url)
print(r.text)
print(r.status_code)
print(r.cookies)
print(r.url)
# 参数保存在params中,params以字典形式保存数据
url = 'tcc.taobao.com/cc/json/mobile_tel_segment.htm'
params={'tel':13909161860}
r=requests.get(url=url,params=params)
print(r.text)
print(r.status_code)
print(r.cookies)
print(r.url)

获取响应结果

import requests

def test_requests():
r = requests.get('www.baidu.com')
print('\n')
print('状态码是:', r.status_code)
print('url是:', r.url)
print('头信息是:', r.headers)
print('cookies是:', r.cookies)
print('text是:', r.text)
print('请求是:', r.request)

05 requests接口测试-post请求

  • post发送form格式请求参数,使用data
  • post发送json格式请求参数,使用json

06 requests接口测试-cookies值

  • 什么是cookies?

相当于临时身份证。

  • 谷歌浏览器抓包

查看请求参数的格式,看Content-Type字段

如:Content-Type: text/html;charset=utf8,表示请求参数是text格式,编码是utf-8

如果是form式,可以查看form data具体发送的参数

在脚本中发送

查看cookies

  • 解码

编码:把码位转换成字节序列(通俗来说:把字符串转换成用于存储或传输的字节序列,python中是.encode())

解码:把字节序列转换成码位(通俗来说:把字节序列转换成人类可读的文本字符串,python中是.decode())

07 requests接口测试-headers值

headers参数不是每个接口都需要添加的,开发可以自行定义。

  • 不需要添加headers
import requests
def test_51job():
# 前程无忧搜索岗位不需要headers信息
url='search.51job.com/list/030200,000000,0000,00,9,99,%25E8%25BD%25AF%25E4%25BB%25B6%25E6%25B5%258B%25E8%25AF%2595%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare='
r_51job=requests.get(url=url)
# print(r_51job.text)
print(r_51job.encoding) # html默认的编码方式是ISO-8859-1
r_51job.encoding='gb2312' # 修改编码方式
print(r_51job.text)
  • 乱码问题
  • 需要添加headers
import requests
def test_12306():
# 12306搜索车票需要添加headers信息,只需要cookies即可。实际工作中,headers需要哪些值,咨询开发人员了解。
url='kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2020-06-06&leftTicketDTO.from_station=SHH&leftTicketDTO.to_station=GZQ&purpose_codes=ADULT'
headers={'Cookie': 'JSESSIONID=C5DCA622147CEF2C4D9C3D3E53E9CF26; BIGipServerotn=1406730506.64545.0000; RAIL_EXPIRATION=1591740743186; RAIL_DEVICEID=FqtrEO_1lQCMdLmY_0uxjBNyLf5esH-V-KtA_I-kPu0j721_HYTxo4IobbdtZbAr75fLtGhLHAQTE8mWEaNmM7ococf3hUVIFw-Gaaper7CzwlrDFsHwwey8w_YQ5gGoMoyfKgVJ5o4nNuWVYhuiC_cxPzsWFJkF; BIGipServerpassport=954728714.50215.0000; route=6f50b51faa11b987e576cdb301e545c4; _jc_save_toStation=%u5E7F%u5DDE%2CGZQ; _jc_save_fromDate=2020-06-06; _jc_save_toDate=2020-06-06; _jc_save_wfdc_flag=dc; _jc_save_fromStation=%u4E0A%u6D77%2CSHH'}
r_12306=requests.get(url=url,headers=headers)
print(r_12306.text)
  • 使用chrome查看headers

08 requests接口测试-www.ctrip.com/'
r=requests.get(url=url,verify=False)
print(r.text)
  • 在verify里面添加证书路径

09 requests接口测试-上传文件files参数

  • 携程上传头像
import requests

def test_files():
url='sinfo.ctrip.com/MyInfo/Ajax/UploadPhoto.ashx' # 携程上传头像
cookies={'cookie': 'cookies的值'}
files={'uploadFile_852':('Markdown.jpg',open('D:\\Markdown.jpg','rb'),'image/jpeg')}
r=requests.post(url=url,files=files,verify=False,cookies=cookies)
print(r.text)

files字段说明:

uploadFile_852 文件参数的名字,通过抓包工具查看,实际工作中直接向开发咨询了解。

Markdown.jpg 文件名

open('D:\Markdown.jpg','rb') 打开本地一个文件,注意使用\转义

image/jpeg 文件格式

  • 禅道上传csv文件

上传文件,你一般需要什么参数?

1.url

2.请求参数

3.请求方式

4.files参数

5.cookies

  • 接口测试两个检查点

1.校验返回结果与开发定义的(接口文档)是否一致,如上传成功返回1,上传失败返回0

2.校验接口逻辑是否正确,如上传文件后会把文件保存在服务器的一个目录下,到该目录下检查是否存在上传的文件

10 requests接口测试-timeout超时

  • 超时测试介绍
  • 超时异常
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='tcc.taobao.com', port=80): Max retries exceeded with url: /cc/json/mobile_tel_segment.htm?tel=13909161860 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x000001576DCE1148>, 'Connection to tcc.taobao.com timed out. (connect timeout=0.01)'))import requests

def test_timeout():
url = 'tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860'
r=requests.get(url=url,timeout=1)
print(r.text)
  • 轮循
import requests

def test_files():
# 携程上传用户头像
for i in range(0,10):
try:
url='sinfo.ctrip.com/MyInfo/Ajax/UploadPhoto.ashx'
cookies={'cookie': 'cookies的值')}
r=requests.post(url=url,files=files,verify=False,cookies=cookies,timeout=0.25)
print(r.text)
except:
print('运行出错')

11 requests接口测试-auth鉴权

12 requests接口测试-文件下载接口

  • 导出禅道用例模板
  • 思路
  • stream参数的使用

13 requests接口测试-session操作

  • 为什么要使用session参数?

有很多接口需要一些公共的参数,比如cookies,可节省时间,精简代码

def test_seesion():
s=Session()
s.headers={'h':'test_headers'}
r=s.post('pay.weixin.qq.com/wiki/doc/api/index.html​​​

​​pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3​​

  • 微信支付签名算法

(签名校验工具)

签名生成的通用步骤如下:

第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA。

特别注意以下重要规则:

◆ 参数名ASCII码从小到大排序(字典序);

◆ 如果参数的值为空不参与签名;

◆ 参数名区分大小写;

◆ 验证调用返回或微信主动通知签名时,传送的sign参数不参与签名,将生成的签名与该sign值作校验。

◆ 微信接口可能增加字段,验证签名时必须支持增加的扩展字段

第二步,在stringA最后拼接上key得到stringSignTemp字符串,并对stringSignTemp进行MD5运算,再将得到的字符串所有字符转换为大写,得到sign值signValue。

◆ key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置

举例:

假设传送的参数如下:

appid: wxd930ea5d5a258f4f

mch_id: 10000100

device_info: 1000

body: test

nonce_str: ibuaiVcKdpRxkhJA

第一步:对参数按照key=value的格式,并按照参数名ASCII字典序排序如下:

stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA";

第二步:拼接API密钥:

stringSignTemp=stringA+"&key=192006250b4c09247ec02edce69f6a2d" //注:key为商户平台设置的密钥key

sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A9CF3B7" //注:MD5签名方式

sign=hash_hmac("sha256",stringSignTemp,key).toUpperCase()="6A9AE1657590FD6257D693A078E1C3E4BB6BA4DC30B23E0EE2496E54170DACD6" //注:HMAC-SHA256签名方式,部分语言的hmac方法生成结果二进制结果,需要调对应函数转化为十六进制字符串。

最终得到最终发送的数据:

<xml>

<appid>wxd930ea5d5a258f4f</appid>

<mch_id>10000100</mch_id>

<device_info>1000</device_info>

<body>test</body>

<nonce_str>ibuaiVcKdpRxkhJA</nonce_str>

<sign>9A0A8659F005D6984697E2CA0A9CF3B7</sign>

</xml>


简单来说,就是通过上面的规则生成sign签名,然后放在sign参数发送。

proxies代理机制

proxies = {'127.0.0.1:8888', '127.0.0.1:8888'}
def test_proxies():
r = requests.get("www.baidu.com/'
sit_ip='cn.bing.com/'
return sit_ip

def sql_conf():
# 定义数据库的配置
ip=''
username=''
password=''
port=3306
charset='utf-8'
return ip,username,password,port,charsetimport requests
from config.conf import * # 从config导入

def test_conf():
url=server_ip()+'/login' # server_ip()表示引入这个函数,而不是这个方法
print(url)
r=requests.get(url=url)
print(r.text)
import requests
from config.conf import * # 从config导入

def test_conf():
url=server_ip()+'/login' #
print(url)
r=requests.get(url=url)
print(r.text)

19 requests接口自动化-excel参数化

20 requests接口自动化-数据库参数化

21 requests接口自动化-列表与字典参数化

22 requests接口自动化-动态关联text/html格式

23 requests接口自动化-动态关联json格式

24 requests接口自动化-assert断言

25 pytest框架-安装

26 pytest框架-运行测试用例

27 pytest框架-动态关联

28 pytest框架-生成测试报告

29 Jenkins+requests+pytest+allure持续集成