在接口测试中,大多数项目的接口是需要登录后进行操作的,经常用到requests库进行模拟登录及登录后的操作,下面是我不断踩坑后总结出来的关于登录凭证cookies的3种操作方法。

一. 用 requests.utils.dict_from_cookiejar() 把返回的cookies转换成字典

  1. 处理cookies:

 1 import requests
2
3 def login():
4 login_url = 'http://www.xxx.com/login
5 headers = {
6 "Accept": "application/json, text/javascript, */*; q=0.01"
7 }
8 body = {
9 "usercode": "liuzz05@****.com",
10 "password": "123456"
11 }
12 try:
13 res = requests.post(url=login_url, headers=headers, data=body)
14 cookies = res.cookies
15
16 cookie = requests.utils.dict_from_cookiejar(cookies)
17
18 return cookie
19 except Exception as err:
20 print('获取cookie失败:\n{0}'.format(err))

  2. 使用cookie:

1 import requests
2
3 def get_data():
4 cookie = login()
5 res = requests.get(url=get_data_url, cookies=cookie)
6 print(res.text)

二. 遍历cookies的键值,拼接成cookie格式

  1. 处理cookies:

 1 import requests
2
3 def login():
4 login_url = 'http://www.xxx.com/login
5 headers = {
6 "Accept": "application/json, text/javascript, */*; q=0.01"
7 }
8 body = {
9 "usercode": "liuzz05@****.com",
10 "password": "123456"
11 }
12 try:
13 res = requests.post(url=login_url, headers=headers, data=body)
14 cookies = res.cookies.items()
15
16 cookie = ''
17 for name, value in cookies:
18 cookie += '{0}={1};'.format(name, value)
19
20 return cookie
21 except Exception as err:
22 print('获取cookie失败:\n{0}'.format(err))

  2. 使用cookie:

1 import requests
2
3 def get_data():
4 cookie = login()
5 headers = {
6 "cookie": cookie
7 }
8 res = requests.get(url=get_data_url, headers=headers)
9 print(res.text)

三. 直接拼接cookies,这种方法比较傻,前提是要知道cookies的键

  1. 处理cookies:

 1 import requests
2
3 def login():
4 login_url = 'http://www.xxx.com/login
5 headers = {
6 "Accept": "application/json, text/javascript, */*; q=0.01"
7 }
8 body = {
9 "usercode": "liuzz05@****.com",
10 "password": "123456"
11 }
12 try:
13 res = requests.post(url=login_url, headers=headers, data=body)
14 cookies = res.cookies
15
16 phpsessid = cookies['phpsessid']
17 env_orgcode = cookies['env_orgcode']
18 acw_tc = cookies['acw_tc']
19 aliyungf_tc = cookies['aliyungf_tc']
20 last_env = cookies['last_env']
21
22 cookie = 'phpsessid={0};env_orgcode={1};acw_tc{2};aliyungf_tc={3};last_env={4}'.format(
23 phpsessid, env_orgcode, acw_tc, aliyungf_tc, last_env
24 )
25
26 return cookie
27 except Exception as err:
28 print('获取cookie失败:\n{0}'.format(err))

  2. 使用cookie:

1 import requests
2
3 def get_data():
4 cookie = login()
5 headers = {
6 "cookie": cookie
7 }
8 res = requests.get(url=get_data_url, headers=headers)
9 print(res.text)

https://www.cnblogs.com/liuzhzhao/p/12114453.html

最新文章

  1. vim - mark
  2. Linux Shell脚本编程--Head/Tail命令详解
  3. mysql操作记录
  4. JS移动端如何监听软键盘回车事件
  5. codevs3196 黄金宝藏
  6. Saltstack系列4:Saltstack之Grains组件
  7. CRM PrincipalObjectAccess(POA)
  8. Sencha Touch 2.4 callParent() 用法
  9. openfire 介绍安装使用
  10. Apache / PHP 5.x Remote Code Execution Exploit
  11. 开始学javascript基础
  12. Sql Server专题:SQL 经典实例
  13. C#复制数据库,将数据库数据转到还有一个数据库
  14. QT5.6 编译SQLServer驱动
  15. 快捷键accesskey
  16. Linux网络基础设施配置
  17. Android简易实战教程--第六话《开发一键锁屏应用2·完成》
  18. 记一次innobackupex备份恢复数据库过程
  19. float和position谁好?
  20. Leet Code 3. Longest Substring Without Repeating Characters

热门文章

  1. oracle 导出导入表 不到出指定表
  2. Python中Websocket的实现及基本原理
  3. 错误:[ERROR] 不再支持源选项 5。请使用 6 或更高版本。 [ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。
  4. CAM更改起始直径为直径
  5. 错误 C2664 “int fputs(const char *,FILE *)”: 无法将参数 1 从“char”转换为“const char *”解决方法
  6. Visual Studio 安装时,共享组件、工具和SDK的路径无法更改解决方法
  7. Ubuntu20.04修改环境变量失误导致开机循环——解决方法以及保存profile
  8. linux环境通过nginx转发allure报告
  9. autohotkey switching within applications
  10. scrcpy不使用adb远程控制android