AlexaController.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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. back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \
  23. communicate(timeout=2)
  24. djangoLogger = logging.getLogger('django')
  25. djangoLogger.info('调用pushtool日志:\n {}'.format(back))
  26. return HttpResponse(back)
  27. except Exception as e:
  28. return HttpResponse(e)