Test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/5/22 13:58
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: Test.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from django.views.generic.base import View
  15. '''
  16. http://192.168.136.40:8077/Test
  17. '''
  18. from Object.ResponseObject import ResponseObject
  19. from Ansjer.config import BASE_DIR
  20. import json
  21. from alipay import AliPay
  22. import time
  23. # 测试接口sdk
  24. class Test(View):
  25. def get(self, request, *args, **kwargs):
  26. import json
  27. import requests
  28. data = {
  29. "key1": "abc",
  30. "key2": "def"
  31. }
  32. json_data = {
  33. "collapse_key": "WhatYouWant",
  34. "data": data,
  35. "delay_while_idle": False,
  36. "time_to_live": 3600,
  37. "registration_ids": [
  38. 'eSooD4fAARg:APA91bEPenBPnSn5aXIQk56QdLOQ1Mu3hevHsekP_0eDpg458y2ZMBP6By2rVsGYIoxZrXxvkkptPKUE9CmUygBxaZXABddUWB9FyLlznRFerC7RG9X5PsEOH58xK9_aTUdkT7p6Ocld']
  39. }
  40. url = 'https://android.googleapis.com/gcm/send'
  41. serverKey = "AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I"
  42. data = json.dumps(json_data).encode('utf-8')
  43. headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % serverKey}
  44. req = requests.post(url, data, headers=headers)
  45. response = ResponseObject()
  46. return response.json(0)
  47. def jgPush(self, request):
  48. response = ResponseObject()
  49. devToken = request.GET.get('devToken', '')
  50. app_key = request.GET.get('app_key', '')
  51. master_secret = request.GET.get('master_secret', '')
  52. import jpush as jpush
  53. # 此处换成各自的app_key和master_secret
  54. _jpush = jpush.JPush(app_key, master_secret)
  55. push = _jpush.create_push()
  56. # if you set the logging level to "DEBUG",it will show the debug logging.
  57. _jpush.set_logging("DEBUG")
  58. # push.audience = jpush.all_
  59. push.audience = jpush.registration_id(devToken)
  60. push.notification = jpush.notification(alert="hello python jpush api")
  61. push.platform = jpush.all_
  62. try:
  63. res = push.send()
  64. except Exception as e:
  65. print("Exception")
  66. return response.json(10, repr(e))
  67. return response.json(0)
  68. def post(self, request, *args, **kwargs):
  69. response = ResponseObject()
  70. data = request.POST.dict()
  71. signature = data["sign"]
  72. data.pop('sign')
  73. print(json.dumps(data))
  74. print(signature)
  75. # verify
  76. app_private_key_string = open(BASE_DIR + '/Controller/alipay_private_2048.pem').read()
  77. alipay_public_key_string = open(BASE_DIR + '/Controller/alipay_public_2048.pem').read()
  78. alipay = AliPay(
  79. appid="2016092200569234",
  80. app_notify_url=None, # the default notify path
  81. app_private_key_string=app_private_key_string,
  82. alipay_public_key_string=alipay_public_key_string,
  83. sign_type="RSA2", # RSA or RSA2
  84. debug=False # False by default
  85. )
  86. success = alipay.verify(data, signature)
  87. if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED"):
  88. print("trade succeed")
  89. return response.json(0, signature)
  90. # 修改 资源改变
  91. def put(self, request):
  92. response = ResponseObject()
  93. return response.json(0, '')
  94. # 修改 属性改变
  95. def PATCH(self, request):
  96. response = ResponseObject()
  97. return response.json(0)
  98. # 删除
  99. def delete(self, request):
  100. response = ResponseObject()
  101. return response.json(0)
  102. def validation(self, request_dict, *args, **kwargs):
  103. response = ResponseObject()
  104. return response.json(0)