标签:utf-8 temp inf bytes sts 去除 login sam key
一般公司对外的接口都会用到sign签名,对不同的客户提供不同的apikey ,这样可以提高接口请求的安全性,避免被人抓包后乱请求。
sign签名是一种很常见的方式
关于sign签名的可以参考前面一篇的介绍https://www.cnblogs.com/yoyoketang/p/11742187.html
一登陆的接口请求为例,如下接口抓包报文信息,其中sign的签名规则如下
POST http://127.0.0.1:8000/api/v3/login HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: 127.0.0.1:8000
Content-Length: 111
{
"username": "test",
"password": "123456",
"sign": "1aca01806e93bb408041965a817666af"
}
HTTP/1.1 200 OK
Date: Sat, 26 Oct 2019 03:38:31 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Content-Type: application/json
Vary: Accept, Cookie
Allow: POST, OPTIONS
X-Frame-Options: SAMEORIGIN
Content-Length: 109
{"code": 0, "msg": "login success!", "username": "test", "token": "a76ba3b8fcbdff82f6a94e5ad5bf8fb934192e5f"}
使用httprunner框架写脚本
- config:
name: logincase
variables: {}
- test:
name: login case1
request:
url: http://127.0.0.1:8000/api/v3/login
method: POST
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: test
password: "123456"
setup_hooks:
- ${setup_request($request)}
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.msg, login success!]
- eq: [content.code, 0]
在debugtalk.py 编写setup_hook函数,对请求的body部分预处理
import hashlib
def sign_body(body, apikey="12345678"):
'''请求body sign签名'''
# 列表生成式,生成key=value格式
a = ["".join(i) for i in body.items() if i[1] and i[0] != "sign"]
# print(a)
# 参数名ASCII码从小到大排序
strA = "".join(sorted(a))
# print(strA)
# 在strA后面拼接上apiKey得到striSignTemp字符串
striSignTemp = strA+apikey
# 将strSignTemp字符串转换为小写字符串后进行MD5运算
# MD5加密
def jiamimd5(src):
m = hashlib.md5()
m.update(src.encode('UTF-8'))
return m.hexdigest()
sign = jiamimd5(striSignTemp.lower())
# print(sign)
return sign
def setup_request(request):
'''setuphook函数,发请求前预处理'''
body = request.get("json")
print(body)
# 由body请求参数生成sign值
sign = sign_body(body, apikey="12345678")
print("sign值:%s" % sign)
request["json"]["sign"] = sign
if __name__ == '__main__':
body = {
"username": "test",
"password": "123456"
}
print(sign_body(body))
运行用例
D:\soft\HELL\DEMO>hrun login_sign_demo.yml
login case1
{'username': 'test', 'password': '123456'}
sign值:1aca01806e93bb408041965a817666af
INFO POST http://127.0.0.1:8000/api/v3/login
INFO status_code: 200, response_time(ms): 689.84 ms, response_length: 109 bytes
INFO start to validate.
.
----------------------------------------------------------------------
Ran 1 test in 0.698s
OK
INFO Start to render Html report ...
INFO Generated Html report: D:\soft\HELL\DEMO\reports\1572062969.html
D:\soft\HELL\DEMO>
标签:utf-8 temp inf bytes sts 去除 login sam key
原文地址:https://www.cnblogs.com/yoyoketang/p/11742464.html