sim_info.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import json
  2. from aliyunsdkcore.vendored.requests.auth import HTTPBasicAuth
  3. import requests
  4. import datetime
  5. import time
  6. import hashlib
  7. def __time_dif(checkdatetime):
  8. """计算时间差"""
  9. nowdatetime = datetime.datetime.now()
  10. checkdatetime = datetime.datetime.strptime(checkdatetime, "%Y-%m-%d %H:%M:%S")
  11. timedif = checkdatetime - nowdatetime
  12. return timedif.days
  13. def sim_updata(iccid):
  14. # 时间戳 用于获取sign
  15. timestamp = int(time.time())
  16. current_milli_time = lambda: int(round(time.time() * 1000))
  17. data_1 = "appid=%s&iccid=%s&timestamp=%s%s"%("102420177762",iccid,current_milli_time(),"6397d7e6a56589f1d93284e9800493e1")
  18. sign = hashlib.sha256(data_1.encode('utf-8')).hexdigest()
  19. data = {"appid": "102420177762", "iccid": iccid, "timestamp":current_milli_time(),"sign":sign}
  20. url = "https://api.simboss.com/2.0/device/detail"
  21. try:
  22. status = 1
  23. ret = requests.post(url, data=data)
  24. code = json.loads(ret.text)["code"]
  25. if code == "0":
  26. status = 1
  27. else:
  28. url = 'http://sim.brlink.cn/api/open/iotcard/card'
  29. appkey = "iaO2DKgS8KdlnVgU"
  30. appsecret = "qzKgO4sBdzMrjRwv9H22S9ufepNv8Hl5ehPqkYVD31DCICjyKwqUdj7zihQQKfgx"
  31. status = 2
  32. ret = requests.post(url,json={'iccid':iccid},auth=HTTPBasicAuth(appkey,appsecret),timeout=(5,10))
  33. codes = json.loads(ret.text)["code"]
  34. if codes == 0:
  35. status = 2
  36. else:
  37. url = "https://jsnl.xmnengjia.com/open/api/module/cards"
  38. data = {"iccids":[iccid]}
  39. data = json.dumps(data)
  40. ret = requests.post(url,data=data,timeout=(10,30))
  41. print(ret.text)
  42. status = 3
  43. except:
  44. status = 0
  45. ret = 0
  46. day = 0
  47. print(status, ret)
  48. if ret:
  49. try:
  50. result = json.loads(ret.text)
  51. expiry_date = result.get("data", {}).get("expiry_date")
  52. now_date = int(time.time())
  53. time_difference = int((expiry_date - now_date) / 3600 / 24)
  54. print(time_difference)
  55. if time_difference < 30:
  56. return [0,"有效期剩余{}天".format(time_difference)]
  57. elif time_difference >= 181:
  58. return [1,"有效期剩余{}天".format(time_difference)]
  59. else:
  60. return [2,"有效期剩余{}天".format(time_difference)]
  61. except:
  62. return [0, "查询无结果"]
  63. else:
  64. return [0, "查询无结果"]
  65. def sim_info(iccid):
  66. """查询卡信息"""
  67. url = "http://8.136.98.49:10001/iotcard/platsimview/inquiries/"
  68. try:
  69. response = requests.request("POST", url, data={"iccid":iccid})
  70. except:
  71. return [0,"查询卡信息异常稍后重试"]
  72. else:
  73. res_data = json.loads(response.text)
  74. if res_data["msg"]=="success" and res_data["data"]:
  75. expiry_date = res_data["data"]["expiry_date"]
  76. if expiry_date == "未知":
  77. return [0,"未查询到卡信息"]
  78. elif expiry_date == "未激活":
  79. return [3, "未激活"]
  80. else:
  81. time_difference = __time_dif(expiry_date)
  82. if time_difference < 30:
  83. return [0,"有效期剩余{}天".format(time_difference)]
  84. elif time_difference >= 181:
  85. return [1,"有效期剩余{}天".format(time_difference)]
  86. else:
  87. return [2,"有效期剩余{}天".format(time_difference)]
  88. else:
  89. return [0,"查询无结果"]
  90. if __name__ == "__main__":
  91. # result = sim_info("89860480192280587662")
  92. # result = sim_updata("89860480192280587662")
  93. result = sim_updata("898604C32622D0173770")
  94. print(result)