main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import requests
  4. import hashlib
  5. import time
  6. from typing import Union
  7. from Crypto.Cipher import AES
  8. def get_sign(ts: Union[str, int], app_key: str, app_secret: str):
  9. ts = str(ts)
  10. hl = hashlib.md5()
  11. p = app_key + app_secret + ts
  12. hl.update(p.encode(encoding='utf-8'))
  13. return hl.hexdigest()
  14. def aesEcbEncrypt(key, src):
  15. size = 16
  16. padding = lambda s: s + (size - len(s.encode()) % size) * chr(size - len(s.encode()) % size)
  17. pdata = padding(src).encode('utf-8')
  18. kdata = key.encode('utf-8')
  19. cip = AES.new(kdata, AES.MODE_ECB)
  20. ret = cip.encrypt(pdata).hex()
  21. return ret
  22. def aesEcbDecrypt(key, src):
  23. size = 16
  24. padtrim = lambda s: s[:-ord(s[len(s) - 1:])]
  25. endata = bytes().fromhex(src)
  26. kdata = key.encode('utf-8')
  27. cip = AES.new(kdata, AES.MODE_ECB)
  28. decData = cip.decrypt(endata)
  29. trimpadData = padtrim(decData)
  30. return trimpadData.decode('utf-8')
  31. def apiVisitBySign(apiUrl, param, secret, enc, appkey):
  32. pdata = json.dumps(param, separators=(',', ':'), ensure_ascii=False)
  33. params = param
  34. if enc:
  35. pdata = aesEcbEncrypt(secret, pdata)
  36. params = {
  37. 'encrypt_data': pdata,
  38. }
  39. ts = str(int(time.time()))
  40. sign = get_sign(ts, appkey, secret)
  41. headers = {
  42. 'Content-Type': 'application/json',
  43. 'timestamp': ts,
  44. "appkey":appkey,
  45. "sign":sign
  46. }
  47. session = requests.session()
  48. res = session.get(apiUrl, params=params, headers=headers, verify=False)
  49. if res.status_code != 200:
  50. print(r.status_code)
  51. return
  52. print('origin res is:', res.text)
  53. jdata = res.json()
  54. if jdata.get('data'):
  55. print(jdata['data'])
  56. if enc:
  57. print('need decrypt')
  58. ret = aesEcbDecrypt(secret, jdata['data'])
  59. print(ret)
  60. def apiVisitByToken(apiUrl, param, secret, enc, token):
  61. pdata = json.dumps(param, separators=(',', ':'), ensure_ascii=False)
  62. params = param
  63. if enc:
  64. pdata = aesEcbEncrypt(secret, pdata)
  65. params = {
  66. 'encrypt_data': pdata,
  67. }
  68. headers = {
  69. 'Content-Type': 'application/json',
  70. 'token': token,
  71. }
  72. session = requests.session()
  73. res = session.get(apiUrl, params=params, headers=headers, verify=False)
  74. if res.status_code != 200:
  75. print(r.status_code)
  76. return
  77. print('origin res is:', res.text)
  78. jdata = res.json()
  79. if jdata.get('data'):
  80. print(jdata['data'])
  81. if enc:
  82. print('need decrypt')
  83. ret = aesEcbDecrypt(secret, jdata['data'])
  84. print(ret)
  85. def getToken(tokenUrl, user, password):
  86. session = requests.session()
  87. params = {
  88. 'user': user,
  89. 'password':password,
  90. }
  91. headers = {
  92. 'Content-Type': 'application/json',
  93. }
  94. res = session.get(tokenUrl, params=params, headers=headers, verify=False)
  95. if res.status_code != 200:
  96. print(res.status_code)
  97. return ''
  98. print('token origin res is', res.text)
  99. jdata = res.json()
  100. return jdata.get('token')
  101. if __name__ == '__main__':
  102. # 填写配置
  103. tokenUrl = 'http://127.0.0.1:41002/api/v1/token'
  104. apiUrl = 'http://127.0.0.1:41002/api/v1/query/2001'
  105. user = '529db83441acff61a054eba562185515'
  106. password = 'DMh7lbyv'
  107. secret = '1749f2a7019db090ca7c9cc69e64033c'
  108. param = {
  109. 'plate_no': '川Y01M21',
  110. 'plate_type': '02',
  111. 'owner': "吴军"
  112. }
  113. tokenType = False
  114. if tokenType:
  115. token = getToken(tokenUrl, user, password)
  116. if token == '':
  117. print('token get failed')
  118. apiVisitByToken(apiUrl, param, secret, True, token)
  119. else:
  120. apiVisitBySign(apiUrl, param, secret, True, user)