set accept-ranges of django app on pythonanywhere

53 views Asked by At

I'm deploying django app to the "pythonanywhere". I have the middleware to set accept-ranges to bytes and it work perfactly well in my localhost, but not in pythonanywhere server.

Is there another way to set accept-ranges in my pythonanywhere server?

Edit

this is my code.(middleware.py)

import os

from django.utils.deprecation import MiddlewareMixin


class RangesMiddleware(MiddlewareMixin):
    def process_response(self, request, response):
        if response.status_code != 200 or not hasattr(response, 'file_to_stream'):
            return response
        http_range = request.META.get('HTTP_RANGE')
        if not (http_range and http_range.startswith('bytes=') and http_range.count('-') == 1):
            return response
        if_range = request.META.get('HTTP_IF_RANGE')
        if if_range and if_range != response.get('Last-Modified') and if_range != response.get('ETag'):
            return response
        f = response.file_to_stream
        statobj = os.fstat(f.fileno())
        start, end = http_range.split('=')[1].split('-')
        if not start:  # requesting the last N bytes
            start = max(0, statobj.st_size - int(end))
            end = ''
        start, end = int(start or 0), int(end or statobj.st_size - 1)
        assert 0 <= start < statobj.st_size, (start, statobj.st_size)
        end = min(end, statobj.st_size - 1)
        f.seek(start)
        old_read = f.read
        f.read = lambda n: old_read(min(n, end + 1 - f.tell()))
        response.status_code = 206
        response['Content-Length'] = end + 1 - start
        response['Content-Range'] = 'bytes %d-%d/%d' % (start, end, statobj.st_size)
        return response

And the settings.py

MIDDLEWARE = [
    'audios.middleware.RangesMiddleware',
    'django.middleware.security.SecurityMiddleware',
    ...
]

(my app name is audios) and there is no error in the both side of the server but it just don't work in pythonanywhere server

0

There are 0 answers