def get_file(request): path = request.GET['path'] if not is_allowed_file(path): return HttpResponse(status=404) response = StreamingHttpResponse() response['Accept-Ranges'] = 'bytes' if not os.path.exists(path.encode('utf-8')): response.status_code = 404 return response if '.flac' in path.lower(): response['Content-Type'] = 'audio/flac' elif '.mp3' in path.lower(): response['Content-Type'] = 'audio/mpeg' response['Content-Length'] = os.path.getsize(path.encode('utf-8')) if request.method == 'HEAD': print 'head' return response file = open(path.encode('utf-8'), 'rb') response.streaming_content = apply_range(request, response, file) return response
def get_file(request): path = request.GET['path'] if not is_allowed_file(path): return HttpResponse(status=404) response = StreamingHttpResponse() response['Accept-Ranges'] = 'bytes' if not os.path.exists(path.encode('utf-8')): response.status_code = 404 return response if '.flac' in path.lower(): response['Content-Type'] = 'audio/flac' elif '.mp3' in path.lower(): response['Content-Type'] = 'audio/mpeg' response['Content-Length'] = os.path.getsize(path.encode('utf-8')) if request.method == 'HEAD': print 'head' return response file = open(path.encode('utf-8'), 'rb') response.streaming_content = apply_range(request, response, file) return response
def get(self, request): resp = StreamingHttpResponse() command_kwargs = { 'shell':False, 'env':None, 'cwd':None } admin_script = getattr(settings,"ADMIN_SCRIPT", {}) command = admin_script.get('args',"") if not command: return resp command_kwargs.update(admin_script, stdout=PIPE, stderr=STDOUT, bufsize=0, close_fds=True, preexec_fn=os.setsid ) resp['Connection'] = "Keep-Alive" doc_start = [ '<!DOCTYPE html>', '<html lang="en">', '<head>', '<meta charset="utf-8">', '<title>output</title>', '<style>body {font-family: monospace; white-space: pre;}</style>', '</head>', '<body>', ] doc_end = [ '<script>parent.done();</script>', '</html>', '</body>' ] scroll_to_bottom = '<script type="text/javascript">window.scrollBy(0,50);</script>' process = Popen(**command_kwargs) # Save the pid in the user's session (a thread-safe place) request.session['pid'] = process.pid def read_output(): for line in iter(process.stdout.readline, b''): yield "%s%s" %(line, scroll_to_bottom) resp.streaming_content = itertools.chain(doc_start, read_output(), doc_end) return resp
def get(self, request): resp = StreamingHttpResponse() command_kwargs = {'shell': False, 'env': None, 'cwd': None} admin_script = getattr(settings, "ADMIN_SCRIPT", {}) command = admin_script.get('args', "") if not command: return resp command_kwargs.update(admin_script, stdout=PIPE, stderr=STDOUT, bufsize=0, close_fds=True, preexec_fn=os.setsid) resp['Connection'] = "Keep-Alive" doc_start = [ '<!DOCTYPE html>', '<html lang="en">', '<head>', '<meta charset="utf-8">', '<title>output</title>', '<style>body {font-family: monospace; white-space: pre;}</style>', '</head>', '<body>', ] doc_end = ['<script>parent.done();</script>', '</html>', '</body>'] scroll_to_bottom = '<script type="text/javascript">window.scrollBy(0,50);</script>' process = Popen(**command_kwargs) # Save the pid in the user's session (a thread-safe place) request.session['pid'] = process.pid def read_output(): for line in iter(process.stdout.readline, b''): yield "%s%s" % (line, scroll_to_bottom) resp.streaming_content = itertools.chain(doc_start, read_output(), doc_end) return resp