AlexaController.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import subprocess
  2. from django.views import View
  3. from django.http import HttpResponse
  4. from django.utils.decorators import method_decorator
  5. from django.views.decorators.csrf import csrf_exempt
  6. class PushCommandView(View):
  7. @method_decorator(csrf_exempt)
  8. def dispatch(self, request, *args, **kwargs):
  9. return super(PushCommandView, self).dispatch(request, *args, **kwargs)
  10. def get(self, request, *args, **kwargs):
  11. request_dict = request.GET
  12. return self.PushCommand(request_dict)
  13. def post(self, request, *args, **kwargs):
  14. request_dict = request.POST
  15. return self.PushCommand(request_dict)
  16. def PushCommand(self, request_dict):
  17. command = request_dict.get('command', None)
  18. if not command:
  19. return HttpResponse(444)
  20. try:
  21. back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \
  22. communicate(timeout=2)
  23. return HttpResponse(back)
  24. except Exception as e:
  25. return HttpResponse(e)