AlexaController.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import logging
  2. import subprocess
  3. from django.views import View
  4. from django.http import HttpResponse
  5. from django.utils.decorators import method_decorator
  6. from django.views.decorators.csrf import csrf_exempt
  7. class PushCommandView(View):
  8. @method_decorator(csrf_exempt)
  9. def dispatch(self, request, *args, **kwargs):
  10. return super(PushCommandView, self).dispatch(request, *args, **kwargs)
  11. def get(self, request, *args, **kwargs):
  12. request_dict = request.GET
  13. return self.PushCommand(request_dict)
  14. def post(self, request, *args, **kwargs):
  15. request_dict = request.POST
  16. return self.PushCommand(request_dict)
  17. def PushCommand(self, request_dict):
  18. command = request_dict.get('command', None)
  19. if not command:
  20. return HttpResponse(444)
  21. try:
  22. djangoLogger = logging.getLogger('info')
  23. djangoLogger.info('开始调用')
  24. back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \
  25. communicate(timeout=2)
  26. djangoLogger.info('调用pushtool日志:\n {}'.format(back))
  27. return HttpResponse(back)
  28. except Exception as e:
  29. return HttpResponse(e)