-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp_MD5_util.py
38 lines (31 loc) · 1.07 KB
/
http_MD5_util.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 用于进行http请求,以及MD5加密,生成签名的工具类
import http.client
import json
import hashlib
import urllib.parse
def build_sign(params, secret_key):
sign = ''
for key in sorted(params.keys()):
sign += key + '=' + str(params[key]) + '&'
data = sign + 'secret_key=' + secret_key
return hashlib.md5(data.encode("utf8")).hexdigest().upper()
def http_get(url, resource, params=''):
conn = http.client.HTTPSConnection(url, timeout=10)
conn.request("GET", resource + '?' + params)
response = conn.getresponse()
data = response.read().decode('utf-8')
return json.loads(data)
def http_post(url, resource, params):
headers = {
"Content-type": "application/x-www-form-urlencoded",
}
conn = http.client.HTTPSConnection(url, timeout=10)
temp_params = urllib.parse.urlencode(params)
conn.request("POST", resource, temp_params, headers)
response = conn.getresponse()
data = response.read().decode('utf-8')
params.clear()
conn.close()
return data