-
Notifications
You must be signed in to change notification settings - Fork 20
/
header_sign.py
82 lines (72 loc) · 2.1 KB
/
header_sign.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
qcc 请求头生成
"""
import hashlib
import hmac
import json
from urllib import parse
class SignTool(object):
def __init__(self):
self.seeds = {
"0": "W",
"1": "l",
"2": "k",
"3": "B",
"4": "Q",
"5": "g",
"6": "f",
"7": "i",
"8": "i",
"9": "r",
"10": "v",
"11": "6",
"12": "A",
"13": "K",
"14": "N",
"15": "k",
"16": "4",
"17": "L",
"18": "1",
"19": "8"
}
self.n = 20
def generate_map_result(self, s):
if not s:
s = "/"
s = s.lower()
s = s + s
k = ''
for i in s:
k += self.seeds[str(ord(i) % 20)]
return k
@staticmethod
def sign_with_hmac(key, s):
return hmac.new(bytes(key, encoding='utf-8'), bytes(s, encoding='utf-8'), hashlib.sha512).hexdigest()
def get_head_key(self, s):
s = s.lower()
map_result = self.generate_map_result(s)
key = self.sign_with_hmac(map_result, s)
return key[10:10 + 20]
def get_head_value(self, url, data=None):
if not url:
url = "/"
if not data:
data = {}
key = url.lower()
# JSON.stringify(data).toLowerCase()
data_s = json.dumps(data, ensure_ascii=False).lower()
enc_data = key + key + data_s
enc_key = self.generate_map_result(key)
result = self.sign_with_hmac(enc_key, enc_data)
return result
def get_header(self, url):
paths = parse.urlparse(url)
uri = paths.path + "?" + paths.query
header_key = self.get_head_key(uri)
header_val = self.get_head_value(uri)
return {header_key: header_val}
sign_tool = SignTool()
if __name__ == '__main__':
print(sign_tool.get_header('https://www.qcc.com/api/elib/getNewCompany?countyCode=&flag=&industry=&isSortAsc=false&pageSize=20&province=&sortField=startdate&startDateEnd='))