-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_web.py
More file actions
69 lines (55 loc) · 2.21 KB
/
py_web.py
File metadata and controls
69 lines (55 loc) · 2.21 KB
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
import urllib.parse
import hashlib
import http.client
import base64
import datetime
URL_PATH = 'https://www.56zhifu.com/user/MerchantAccFileDown.do'
Content_Type = 'application/x-www-form-urlencoded'
MD5Key = '7JGHPG950BSAN3EEDU3PY46C'
current_Date_Formatted = datetime.datetime.today().strftime('%Y%m%d')
print('current date: ' + str(current_Date_Formatted))
previous_Date = datetime.datetime.today() - datetime.timedelta(days=1)
previous_Date_Formatted = previous_Date.strftime('%Y%m%d') # format the date to yyyymmdd
print('the day before the current day:', previous_Date_Formatted)
billDate = previous_Date_Formatted
merchantId = '00000000035349'
signType = 'MD5'
version = '2.0'
param = 'billDate=' + billDate + '&' + 'merchantId=' + merchantId + '&' + 'signType=' + signType + '&' + 'version=' \
+ version + MD5Key
print('md5 param :' + param)
res = hashlib.md5(param.encode('utf-8'))
print('md5 res: ' + res.hexdigest())
data = urllib.parse.urlencode({'billDate': billDate, 'merchantId': merchantId, 'signType': signType, 'version': version,
'signature': res.hexdigest()})
data = data.encode('UTF-8')
print('last request data :', data)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/xml"}
try:
conn = http.client.HTTPSConnection("www.56zhifu.com")
conn.request("POST", URL_PATH, data, headers)
response = conn.getresponse()
print(response.status, response.reason)
if response.status != 200:
print(f'Failure to send !')
print(f'Response ', response.reason)
f = response.read()
print('res headers:', response.getheaders())
print('len of res :', len(f))
# print('origin of data: \n', f)
id = f.index(b'retCode=')
retCode = f[id + 9: id + 13]
id = f.index(b'billDate=')
Date = f[id + 10: id + 18]
id = f.index(b'filecontent=')
content = f[id + 13: len(f) - 3]
print('str of data: \n', retCode, Date)
print('decode content: ', base64.b64decode(content))
filePath = "./" + "swt-" + Date.decode('ASCII') + ".txt"
file = open(filePath, "w")
file.write(base64.b64decode(content).decode('utf-8'))
file.close()
conn.close()
except Exception as e:
print(e)
conn.close()