def test_streaming_response(self): r = StreamingHttpResponse(iter(['hello', 'world'])) # iterating over the response itself yields bytestring chunks. chunks = list(r) self.assertEqual(chunks, [b'hello', b'world']) for chunk in chunks: self.assertIsInstance(chunk, six.binary_type) # and the response can only be iterated once. self.assertEqual(list(r), []) # even when a sequence that can be iterated many times, like a list, # is given as content. r = StreamingHttpResponse(['abc', 'def']) self.assertEqual(list(r), [b'abc', b'def']) self.assertEqual(list(r), []) # streaming responses don't have a `content` attribute. self.assertFalse(hasattr(r, 'content')) # and you can't accidentally assign to a `content` attribute. with self.assertRaises(AttributeError): r.content = 'xyz' # but they do have a `streaming_content` attribute. self.assertTrue(hasattr(r, 'streaming_content')) # that exists so we can check if a response is streaming, and wrap or # replace the content iterator. r.streaming_content = iter(['abc', 'def']) r.streaming_content = (chunk.upper() for chunk in r.streaming_content) self.assertEqual(list(r), [b'ABC', b'DEF']) # coercing a streaming response to bytes doesn't return a complete HTTP # message like a regular response does. it only gives us the headers. r = StreamingHttpResponse(iter(['hello', 'world'])) self.assertEqual( six.binary_type(r), b'Content-Type: text/html; charset=utf-8') # and this won't consume its content. self.assertEqual(list(r), [b'hello', b'world']) # additional content cannot be written to the response. r = StreamingHttpResponse(iter(['hello', 'world'])) with self.assertRaises(Exception): r.write('!') # and we can't tell the current position. with self.assertRaises(Exception): r.tell() r = StreamingHttpResponse(iter(['hello', 'world'])) self.assertEqual(r.getvalue(), b'helloworld')
def get_download_response(payload, content_length, content_format, filename, request=None): """ :param payload: File like object. :param content_length: Size of payload in bytes :param content_format: ``couchexport.models.Format`` instance :param filename: Name of the download :param request: The request. Used to determine if a range response should be given. :return: HTTP response """ ranges = None if request and "HTTP_RANGE" in request.META: try: ranges = parse_range_header(request.META['HTTP_RANGE'], content_length) except ValueError: pass if ranges and len(ranges.ranges) != 1: ranges = None response = StreamingHttpResponse(content_type=content_format.mimetype) if content_format.download: response['Content-Disposition'] = safe_filename_header(filename) response["Content-Length"] = content_length response["Accept-Ranges"] = "bytes" if ranges: start, stop = ranges.ranges[0] if stop is not None and stop > content_length: # requested range not satisfiable return HttpResponse(status=416) response.streaming_content = RangedFileWrapper(payload, start=start, stop=stop or float("inf")) end = stop or content_length response["Content-Range"] = "bytes %d-%d/%d" % (start, end - 1, content_length) response["Content-Length"] = end - start response.status_code = 206 else: response.streaming_content = FileWrapper(payload) return response
def download_file(request, dir_name, file_name="", *args, **kwargs): """allows authorized user to download a given file""" # dir_name = file_tuple[0] # file_name = file_tuple[1] if file_name == "": file_name = dir_name dir_name = "." if os.path.sep in file_name: raise PermissionDenied() if request.user.is_authenticated: directory = os.path.join(settings.CLOUD_DIR, request.user.username) # make sure that file exists within current directory file_path = os.path.join(directory, dir_name, file_name) if os.path.isfile(file_path): response = StreamingHttpResponse( content_type='application/octet-stream') response[ 'Content-Disposition'] = 'attachment; filename=%s' % file_name file_obj = open(file_path, 'rb') response.streaming_content = read_file_chunkwise(file_obj) return response else: raise Http404(file_path) else: raise Http404
def large_csv(request): response = StreamingHttpResponse(content_type='text/csv') response['Content-Disposition'] = "attachment;filename='large.csv" rows = ("Row {} {}\n".format(row, row) for row in range(0, 10000)) # response.streaming_content = ("username, age\n", "sasda, 18\n") response.streaming_content = rows return response
def _download_file(request, file_path): """allows authorized user to download a given file""" if check_access(request): response = StreamingHttpResponse(content_type='application/force-download') response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(file_path) file_obj = open(file_path) response.streaming_content = read_file_chunkwise(file_obj) return response
def view_large_csv(request): response = StreamingHttpResponse(content_type='text/csv') response['Content-Disposition'] = "attachent;filename=data.csv" # response.streaming_content=('username,age\n','zhangsan,18\n') rows=("name{},{}\n".format(row,row) for row in range(0,2000)) response.streaming_content=rows return response
def _download_file(request, file_path): """allows authorized user to download a given file""" if check_access(request): response = StreamingHttpResponse(content_type='application/force-download') response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(file_path) file_obj = open(file_path, 'rb') response.streaming_content = read_file_chunkwise(file_obj) return response
def return_file( request, path: str, storage_name: Optional[str] = None ) -> Union[HttpResponseNotFound, StreamingHttpResponse, HttpResponse]: try: if storage_name: storage = get_object_or_404(backends.Storage, name=storage_name) else: storage = None data_item = get_object_or_404(coverages.ArrayDataItem, location=path, storage=storage) ranges = request.headers.get("Range", None) _, ranges = parse_ranges(ranges) size = verify_file(data_item, ranges) if request.method == "GET": response = StreamingHttpResponse( iter_file(data_item, ranges=ranges)) if not ranges: response["Content-Type"] = "image/tiff" elif len(ranges) == 1: _r = ranges[0] response["Content-Type"] = "image/tiff" response["Content-Range"] = f"bytes {_r[0]}-{_r[1]}/{size}" response.status_code = 206 else: boundary = generate_boundary() response.streaming_content = wrap_streaming_content( response.streaming_content, ranges, boundary, size) response[ "Content-Type"] = f"multipart/byteranges; boundary={boundary.decode()}" response.status_code = 206 else: response = HttpResponse("") response["Content-Type"] = "image/tiff" response["Content-Length"] = str(size) response["Access-Control-Allow-Origin"] = "*" response["Accept-Ranges"] = "bytes" response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS, HEAD" response[ "Access-Control-Allow-Headers"] = "X-PINGOTHER, Content-Type, Range" response["Access-Control-Max-Age"] = "86400" return response except Http404: return HttpResponseNotFound("<h1>404 file not found</h1>") except RangeNotSatisfiable: response = HttpResponse("<h1>416 requested range not satisfiable<h1>") response.status_code = 416 return response
def user_mana_key(request): """ 管理用户RSA密钥 * 参数 ** generate - 生成新密钥 ** download - 下载私钥 ** passphrase - 私钥密码 ** puttykey - putty格式 """ msg_prefix = u"管理用户RSA密钥 " req_dict = post_data_to_dict(request.data) user = request.user generate = smart_get(req_dict, 'generate', bool, False) download = smart_get(req_dict, 'download', bool, False) passphrase = smart_get(req_dict, 'passphrase', str, '') puttykey = smart_get(req_dict, 'puttykey', bool, False) try: # admin系统内置账户不可更换密钥 if user.username == 'admin': generate = False if generate: user_gen_rsakey(user, key_expiration=timezone.now() + timezone.timedelta(days=ExUser.KEY_LIFE)) if download: key = get_rsakey_from_string(user.ssh_key_str, passphrase=ExUser.KEY_PASSPHRASE) if puttykey: key_filename = "putty_pkey.ppk" key_string = toString_PUTTY_private(key, passphrase=passphrase) else: key_filename = "{username}.id_rsa".format( username=user.username) key_string = get_key_string(key, passphrase=passphrase) response = StreamingHttpResponse( content_type='application/octet-stream') response[ 'Content-Disposition'] = "attachment; filename={filename}".format( filename=key_filename) response.streaming_content = key_string return response except Exception, e: if isinstance(e, APIException): raise e msg = msg_prefix + u"失败, 错误信息: " + unicode(e) logger.error(format_exc()) return Response({ "status": -1, "msg": msg, "data": {} }, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def csv3(request): ''' 生成大的csv文件 ''' response = StreamingHttpResponse(content_type='text/csv') response['Content-Disposition'] = "attachment;filename=large.csv" # response.streaming_content = ("username,age\n", "ku_rong,18\n") # 如果圆括号中使用for in 循环,那么它就会成为一个生成器 row = ("Row {}.{}\n".format(row, row) for row in range(1, 100000)) response.streaming_content = row return response
def get_download_response(payload, content_length, content_format, filename, request=None): """ :param payload: File like object. :param content_length: Size of payload in bytes :param content_format: ``couchexport.models.Format`` instance :param filename: Name of the download :param request: The request. Used to determine if a range response should be given. :return: HTTP response """ ranges = None if request and "HTTP_RANGE" in request.META: try: ranges = parse_range_header(request.META['HTTP_RANGE'], content_length) except ValueError: pass if ranges and len(ranges.ranges) != 1: ranges = None response = StreamingHttpResponse(content_type=content_format.mimetype) if content_format.download: response['Content-Disposition'] = safe_filename_header(filename) response["Content-Length"] = content_length response["Accept-Ranges"] = "bytes" if ranges: start, stop = ranges.ranges[0] if stop is not None and stop > content_length: # requested range not satisfiable return HttpResponse(status=416) response.streaming_content = RangedFileWrapper(payload, start=start, stop=stop or float("inf")) end = stop or content_length response["Content-Range"] = "bytes %d-%d/%d" % (start, end - 1, content_length) response["Content-Length"] = end - start response.status_code = 206 else: response.streaming_content = FileWrapper(payload) return response
def dispatch(self, request, *args, **kwargs): if request.method.lower() in self.http_method_names: handler = getattr(self, request.method.lower(), self.http_method_not_allowed) else: handler = self.http_method_not_allowed self.request = request self.args = args self.kwargs = kwargs self.timeout = self.kwargs.get('channel') response = HttpResponse() response.streaming_content = self._iterator(handler) response['Cache-Control'] = 'no-cache' return response
def front_seven(request): '''生成大型的 csv 文件''' response = StreamingHttpResponse(content_type="text/csv") response['Content-Disposition'] = "attachment; filename='large_file.csv'" rows = ("{} : {} \t".format(i, i**2) for i in range(10000)) # 必须返回一个生成器 可迭代的对象 response.streaming_content = rows ''' 这个类没有属性content,相反是streaming_content。 这个类的streaming_content必须是一个可以迭代的对象。 StreamingHttpResponse会启动一个进程来和客户端保持长连接, 所以会很消耗资源。所以如果不是特殊要求,尽量少用这种方法。 ''' return response
def get(self, request, format=None): """ Returns a list of API features """ response = StreamingHttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="numbers-formatted.csv"' response.status_code = status.HTTP_200_OK phone = request.GET.get('number') list_of_files = glob.glob('./*.csv') latest_file = max(list_of_files, key=os.path.getctime) result = services.locate_nearest_numbers('+' + phone, latest_file) processes = result[1] queue = result[0] response.streaming_content = chain( header_1(), response_iterator_1(processes, queue, chunk_size=100)) return response
def download_view(request): if request.method == "GET": return render(request, "download.html") if request.POST["mb"] == "-1": # Intentionally generate an exception. _LOG.info("mb=-1 passed in.") print(math.sqrt(-1)) buffer1k = _random_str(1023) + "/" mb = max(int(request.POST["mb"]), 1) ops = int(request.POST.get("ops", 0)) _LOG.info("Start generating %dMB data now (with ops=%d)...", mb, ops) response = StreamingHttpResponse() response["Content-Type"] = "application/binary" response["Content-Disposition"] = 'attachment; filename="random{0}-{1}MB.bin"'.format(random.randint(10, 99), mb) response["Content-Length"] = str(1024 * 1024 * mb) response.streaming_content = _repeat_and_wait(buffer1k, 1024 * mb, ops) logging.info("Passing the generator to the response.") return response
def download_file(request, path): if request.method == 'GET': virtual_root = get_abs_root() eventual_path = get_abs_path(os.path.join(virtual_root, path)) if not is_inside_root(eventual_path): # Someone is playing tricks with .. or %2e%2e or so raise Http404 if os.path.isfile(eventual_path): response = StreamingHttpResponse( content_type='application/force-download') response[ 'Content-Disposition'] = 'attachment; filename=%s' % os.path.basename( eventual_path) file_obj = open(eventual_path, 'rb') response.streaming_content = read_file_chunkwise(file_obj) return response else: raise Http404
def download_file(request, file_name): """allows authorized user to download a given file""" if os.path.sep in file_name: raise PermissionDenied() if check_access(request): directory = settings.DIRECTORY_DIRECTORY #make sure that file exists within current directory files = get_file_names(directory) if file_name in files: file_path = os.path.join(directory, file_name) response = StreamingHttpResponse(mimetype='application/force-download') response['Content-Disposition'] = 'attachment; filename=%s' % file_name file_obj = open(os.path.join(directory, file_name)) response.streaming_content = read_file_chunkwise(file_obj) return response else: raise Http404
def export_file(request, pk, file_name): """ Allows authorized user to export a file. Adapted from https://github.com/ASKBOT/django-directory """ export = get_object_or_404(Export, pk=pk) if (request.user == export.user) or request.user.is_superuser: filepath = os.path.join(export.path, file_name) log.debug("Exporting %s", filepath) if os.path.exists(filepath): response = StreamingHttpResponse() response['Content-Disposition'] = 'attachment; filename=%s' % file_name file_obj = open(filepath) response.streaming_content = _read_file_chunkwise(file_obj) return response else: raise Http404 else: raise PermissionDenied
def test_streaming_response(self): filename = os.path.join(os.path.dirname(__file__), 'abc.txt') # file isn't closed until we close the response. file1 = open(filename) r = StreamingHttpResponse(file1) self.assertFalse(file1.closed) r.close() self.assertTrue(file1.closed) # when multiple file are assigned as content, make sure they are all # closed with the response. file1 = open(filename) file2 = open(filename) r = StreamingHttpResponse(file1) r.streaming_content = file2 self.assertFalse(file1.closed) self.assertFalse(file2.closed) r.close() self.assertTrue(file1.closed) self.assertTrue(file2.closed)
def test_streaming_response(self): filename = os.path.join(os.path.dirname(upath(__file__)), 'abc.txt') # file isn't closed until we close the response. file1 = open(filename) r = StreamingHttpResponse(file1) self.assertFalse(file1.closed) r.close() self.assertTrue(file1.closed) # when multiple file are assigned as content, make sure they are all # closed with the response. file1 = open(filename) file2 = open(filename) r = StreamingHttpResponse(file1) r.streaming_content = file2 self.assertFalse(file1.closed) self.assertFalse(file2.closed) r.close() self.assertTrue(file1.closed) self.assertTrue(file2.closed)
def export_file(request, pk, file_name): """ Allows authorized user to export a file. Adapted from https://github.com/ASKBOT/django-directory """ export = get_object_or_404(Export, pk=pk) if (request.user == export.user) or request.user.is_superuser: filepath = os.path.join(export.path, file_name) log.debug("Exporting %s", filepath) if os.path.exists(filepath): response = StreamingHttpResponse() response[ 'Content-Disposition'] = 'attachment; filename=%s' % file_name file_obj = open(filepath) response.streaming_content = _read_file_chunkwise(file_obj) return response else: raise Http404 else: raise PermissionDenied
def post(self, request): file_serializer = FileSerializer(data=request.data) if file_serializer.is_valid(): file_serializer.save() response = StreamingHttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="numbers-formatted.csv"' response.status_code = status.HTTP_200_OK result = services.reformat_numbers(file_serializer.data['numbers']) processes = result[1] queue = result[0] response.streaming_content = chain( header_2(), response_iterator_2(processes, queue, chunk_size=1000)) return response else: return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def download_file(request, file_name): """allows authorized user to download a given file""" if os.path.sep in file_name: raise PermissionDenied() if check_access(request): directory = settings.DIRECTORY_DIRECTORY #make sure that file exists within current directory files = get_file_names(directory) if file_name in files: file_path = os.path.join(directory, file_name) response = StreamingHttpResponse( mimetype='application/force-download') response[ 'Content-Disposition'] = 'attachment; filename=%s' % file_name file_obj = open(os.path.join(directory, file_name)) response.streaming_content = read_file_chunkwise(file_obj) return response else: raise Http404
def test_streaming_response(self): r = StreamingHttpResponse(iter(["hello", "world"])) # iterating over the response itself yields bytestring chunks. chunks = list(r) self.assertEqual(chunks, [b"hello", b"world"]) for chunk in chunks: self.assertIsInstance(chunk, six.binary_type) # and the response can only be iterated once. self.assertEqual(list(r), []) # even when a sequence that can be iterated many times, like a list, # is given as content. r = StreamingHttpResponse(["abc", "def"]) self.assertEqual(list(r), [b"abc", b"def"]) self.assertEqual(list(r), []) # iterating over Unicode strings still yields bytestring chunks. r.streaming_content = iter(["hello", "café"]) chunks = list(r) # '\xc3\xa9' == unichr(233).encode('utf-8') self.assertEqual(chunks, [b"hello", b"caf\xc3\xa9"]) for chunk in chunks: self.assertIsInstance(chunk, six.binary_type) # streaming responses don't have a `content` attribute. self.assertFalse(hasattr(r, "content")) # and you can't accidentally assign to a `content` attribute. with self.assertRaises(AttributeError): r.content = "xyz" # but they do have a `streaming_content` attribute. self.assertTrue(hasattr(r, "streaming_content")) # that exists so we can check if a response is streaming, and wrap or # replace the content iterator. r.streaming_content = iter(["abc", "def"]) r.streaming_content = (chunk.upper() for chunk in r.streaming_content) self.assertEqual(list(r), [b"ABC", b"DEF"]) # coercing a streaming response to bytes doesn't return a complete HTTP # message like a regular response does. it only gives us the headers. r = StreamingHttpResponse(iter(["hello", "world"])) self.assertEqual(six.binary_type(r), b"Content-Type: text/html; charset=utf-8") # and this won't consume its content. self.assertEqual(list(r), [b"hello", b"world"]) # additional content cannot be written to the response. r = StreamingHttpResponse(iter(["hello", "world"])) with self.assertRaises(Exception): r.write("!") # and we can't tell the current position. with self.assertRaises(Exception): r.tell() r = StreamingHttpResponse(iter(["hello", "world"])) self.assertEqual(r.getvalue(), b"helloworld")
def large_csv_view(request): response = StreamingHttpResponse(content_type='text/csv') response['Content-Disposition'] = "attachment; filename=abc" rows = ("Row {}, {}\n".format(row, row) for row in range(0, 100000)) response.streaming_content = rows return response
def large_csv_file(request): resp = StreamingHttpResponse(content_type='application/pdf') resp['Content-Disposition'] = 'attachment; filename=large.pdf' rows = (f"Row: {row}, {row}\n" for row in range(100000)) resp.streaming_content = rows return resp
def test_streaming_response(self): r = StreamingHttpResponse(iter(["hello", "world"])) # iterating over the response itself yields bytestring chunks. chunks = list(r) self.assertEqual(chunks, [b"hello", b"world"]) for chunk in chunks: self.assertIsInstance(chunk, bytes) # and the response can only be iterated once. self.assertEqual(list(r), []) # even when a sequence that can be iterated many times, like a list, # is given as content. r = StreamingHttpResponse(["abc", "def"]) self.assertEqual(list(r), [b"abc", b"def"]) self.assertEqual(list(r), []) # iterating over strings still yields bytestring chunks. r.streaming_content = iter(["hello", "café"]) chunks = list(r) # '\xc3\xa9' == unichr(233).encode() self.assertEqual(chunks, [b"hello", b"caf\xc3\xa9"]) for chunk in chunks: self.assertIsInstance(chunk, bytes) # streaming responses don't have a `content` attribute. self.assertFalse(hasattr(r, "content")) # and you can't accidentally assign to a `content` attribute. with self.assertRaises(AttributeError): r.content = "xyz" # but they do have a `streaming_content` attribute. self.assertTrue(hasattr(r, "streaming_content")) # that exists so we can check if a response is streaming, and wrap or # replace the content iterator. r.streaming_content = iter(["abc", "def"]) r.streaming_content = (chunk.upper() for chunk in r.streaming_content) self.assertEqual(list(r), [b"ABC", b"DEF"]) # coercing a streaming response to bytes doesn't return a complete HTTP # message like a regular response does. it only gives us the headers. r = StreamingHttpResponse(iter(["hello", "world"])) self.assertEqual(bytes(r), b"Content-Type: text/html; charset=utf-8") # and this won't consume its content. self.assertEqual(list(r), [b"hello", b"world"]) # additional content cannot be written to the response. r = StreamingHttpResponse(iter(["hello", "world"])) with self.assertRaises(Exception): r.write("!") # and we can't tell the current position. with self.assertRaises(Exception): r.tell() r = StreamingHttpResponse(iter(["hello", "world"])) self.assertEqual(r.getvalue(), b"helloworld")
def streaming1_csv_view(request): response = StreamingHttpResponse(content_type="text/csv") response['Content-Disposition'] = "attachment;filename=large.csv" rows = ("p{},{}\n".format(x,x) for x in range(0,1000000)) response.streaming_content = rows return response
def stream_response(content, filename): response = StreamingHttpResponse(content_type='application/octet-stream') response['Content-Disposition'] = "attachment; filename={filename}".format( filename=filename) response.streaming_content = content return response
def search(request): def genrgb(str, malformed): intcol = binascii.crc32(str.strip()) if not malformed: r = "%s" % colors[intcol % len(colors)] else: r = "%s" % colors_red[intcol % len(colors_red)] return r ret = {} ret["result"] = "unknown" ret["msg"] = "unknown msg" ret["rows"] = [] response = StreamingHttpResponse(content_type='application/json') try: where = [] QueryDict = request.GET limit = QueryDict.get('rows') if limit is None or limit == "": limit = 10 colors = list( Color("LightGreen").range_to( Color("MediumAquaMarine", luminance=0.9), 1000)) colors_red = list( Color("Salmon", luminance=0.5).range_to(Color("Tomato", luminance=0.9), 1000)) page = QueryDict.get('page') if page is None or page == "": page = 1 offset = int(max(int(page) - 1, 0)) * int(limit) grouping = QueryDict.get('grouping') if grouping is None: grouping = "1" malformed = QueryDict.get('malformed') if malformed == "1": where.append("malformed = 1") src_ip = QueryDict.get('src_ip') if src_ip is not None and src_ip != "": where.append("source_ip = INET_ATON('%s') " % MySQLdb.escape_string(src_ip)) dst_ip = QueryDict.get('dst_ip') if dst_ip is not None and dst_ip != "": where.append("destination_ip = INET_ATON('%s') " % MySQLdb.escape_string(dst_ip)) sip_callid = QueryDict.get('sip_callid') if sip_callid is not None and sip_callid != "": where.append( "(callid = '%(callid)s' OR callid_rc = '%(callid)s') " % {"callid": MySQLdb.escape_string(sip_callid)}) sip_method = QueryDict.get('sip_method') if sip_method is not None and sip_method != "": where.append("request_method = '%s' " % MySQLdb.escape_string(sip_method)) from_user = QueryDict.get('from_user') if from_user is not None and from_user != "": where.append("(" "`from` = '%(from)s' " "OR `from` = '+%(from)s' " "OR `from` = '+1%(from)s' " "OR `from` = '1%(from)s'" "OR `from` like '%(from)s'" "OR `from` like '+%(from)s'" "OR `from` like '+1%(from)s'" "OR `from` like '1%(from)s'" ")" % {"from": MySQLdb.escape_string(from_user)}) to_user = QueryDict.get('to_user') if to_user is not None and to_user != "": where.append("(" "`to` = '%(to)s' " "OR `to` = '+1%(to)s' " "OR `to` = '+%(to)s' " "OR `to` = '1%(to)s'" "OR `to` like '%(to)s'" "OR `to` like '+%(to)s'" "OR `to` like '+1%(to)s'" "OR `to` like '1%(to)s'" ")" % {"to": MySQLdb.escape_string(to_user)}) date_start = QueryDict.get('date_start') if date_start is not None and date_start != "": where.append("sc.datetime >= '%s' " % MySQLdb.escape_string(date_start)) date_end = QueryDict.get('date_end') if date_end is not None and date_end != "": where.append("sc.datetime <= '%s' " % MySQLdb.escape_string(date_end)) if not where: where.append("1=1") sql = """ SELECT SQL_CALC_FOUND_ROWS sc.id, CAST(UNIX_TIMESTAMP(sc.datetime) AS SIGNED) as datetime, CONCAT(INET_NTOA(sc.source_ip), ':', IFNULL(sc.source_port, '0')) as source_ip, CONCAT(INET_NTOA(sc.destination_ip), ':', IFNULL(sc.destination_port, '0') ) as destination_ip, sc.callid, CONCAT(COALESCE(sc.request_method, ''), COALESCE(sc.response_code, '')) as method, sc.from, sc.to, sc.malformed FROM sip_capture sc WHERE %s %s ORDER BY sc.datetime ASC, sc.id ASC LIMIT %u OFFSET %u """ % (" \n AND ".join(where), "" if grouping == "0" else "GROUP BY sc.callid", int(limit), int(offset)) db = dbConn() cursor = db.cursor() try: cache_key = sha1(sql).hexdigest() #cache.delete(cache_key) c = cache.get(cache_key) if c is None: cursor.execute(sql) print(cursor._last_executed) if cursor.rowcount == 0: raise ObjectDoesNotExist("Records not found") res = {} results = cursor.fetchall() res['results'] = results cursor.execute("SELECT FOUND_ROWS()") found_rows = cursor.fetchone() res['found_rows'] = found_rows cache.add(cache_key, res, 30) else: found_rows = c['found_rows'] results = c['results'] #print sql #cursor.execute(sql) #print cursor._last_executed for row in results: ret["rows"].append({ 'id': row[0], 'date': row[1], 'source_ip': row[2], 'destination_ip': row[3], 'callid': row[4], 'method': row[5], 'from_user': row[6], 'to_user': row[7], 'color': genrgb("%s %s" % (row[2], row[3]), row[8]) }) ret["result"] = "success" ret["msg"] = "success msg" ret["total"] = found_rows[0] except ObjectDoesNotExist as e: ret["result"] = "warning" ret["msg"] = str(e) response.status_code = 404 pass except Exception as e: ret["result"] = "error" ret["msg"] = repr(e) ret['traceback'] = traceback.format_exc() response.status_code = 500 pass finally: cursor.close() except Exception as e: ret["result"] = "error" ret['traceback'] = traceback.format_exc() ret["msg"] = repr(e) response.status_code = 500 json_data = json.dumps(ret) response.streaming_content = json_data return response