AlexaController.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. import json
  5. import random
  6. import subprocess
  7. from django.views import View
  8. from django.utils.decorators import method_decorator
  9. from django.views.decorators.csrf import csrf_exempt
  10. from AnsjerUIDManage.config import SALES, ONLINE_DEVICE
  11. from Object.ResponseObject import ResponseObject
  12. class PushCommandView(View):
  13. @method_decorator(csrf_exempt)
  14. def dispatch(self, request, *args, **kwargs):
  15. return super(PushCommandView, self).dispatch(request, *args, **kwargs)
  16. def get(self, request, *args, **kwargs):
  17. request_dict = request.GET
  18. return self.PushCommand(request_dict)
  19. def post(self, request, *args, **kwargs):
  20. request_dict = request.POST
  21. return self.PushCommand(request_dict)
  22. def PushCommand(self, request_dict):
  23. response = ResponseObject()
  24. command = request_dict.get('command', None)
  25. if not command:
  26. return response(444)
  27. try:
  28. back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \
  29. communicate(timeout=2)
  30. return response.json(0, back)
  31. except Exception as e:
  32. return response(500, repr(e))