首页 > 大数据 > 数据分析 > python对接口中的数据进行md5加密
最近有学员向笔者多次反应, 测试过程 中,比如登录,登录时密码一般是经过加密之后再进行登录的,如果在进行测试时填写的密码是明文的话,那么进行接口测试时必然是失败的,那么要经过怎样的处理才能登录成功呢?
那么今天先简单处理密码是 md5 加密的情况下,使用 python 进行接口测试时, python 如何处理;
当前使用的是 python3.7 版本;
Python3.7 在处理 md5 加密时,需要导入模块 hashlib;
python 的 hashlib 提供了常见的摘要算法,如 MD5,SHA1 等等。
先来看一下 python 是如何把字符串加密成 MD5 字符串的;
import hashlib
def MD5_demo(str):
md= hashlib.md5()# 创建md5对象
md .update(str.encode(encoding= 'utf-8' ))
return md.hexdigest()
if __name__== "__main__" :
# 待加密信息
str = '123456'
md5_str = MD5_demo(str)
print( '加密后为 :' + md5_str)
hexdigest() 在英语中 hex 有十六进制的意思,因此该方法是返回摘要,作为十六进制数据字符串值
注意: update(str.encode(encoding= 'utf-8' )) 这个函数里面需要对字符串进行编码,否则会报 TypeError: Unicode-objects must be encoded before hashing
下面以禅道登陆接口为例进行处理:
通过 fiddler抓包发现,登陆的密码是加密处理的:
以下是代码处理结果:
import requests
import hashlib
def MD5_login(str):
zt_pwd = hashlib.md5()
zt_pwd.update(str.encode(encoding= 'utf-8' ))
return zt_pwd.hexdigest()
password = '123456' #登陆的用户密码=='123456'
url = 'http://192.168.1.105:81/zentao/user-login-L3plbnRhby8=.html'
data = { 'account' : 'admin' , 'password' :MD5_login(password), 'referer' : '/zentao/' }
response = requests.post(url,data=data) # 发送post请求
print(response.content.decode( 'utf-8' ))
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69940641/viewspace-2907851/ ,如需转载,请注明出处,否则将追究法律责任。