#!/usr/bin/env python3 # -*- coding: utf-8 -*- import datetime import json import random import subprocess from django.views import View from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt from AnsjerUIDManage.config import SALES, ONLINE_DEVICE from Object.ResponseObject import ResponseObject class PushCommandView(View): @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): return super(PushCommandView, self).dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): request_dict = request.GET return self.PushCommand(request_dict) def post(self, request, *args, **kwargs): request_dict = request.POST return self.PushCommand(request_dict) def PushCommand(self, request_dict): response = ResponseObject() command = request_dict.get('command', None) if not command: return response(444) try: back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \ communicate(timeout=2) return response.json(0, back) except Exception as e: return response(500, repr(e))