import logging import subprocess from django.views import View from django.http import HttpResponse from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt 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): command = request_dict.get('command', None) if not command: return HttpResponse(444) try: djangoLogger = logging.getLogger('info') djangoLogger.info('开始调用') back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \ communicate(timeout=2) djangoLogger.info('调用pushtool日志:\n {}'.format(back)) return HttpResponse(back) except Exception as e: return HttpResponse(e)