Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Determine the best content to send in an HTTP response

Project Description

accept-types helps your application respond to a HTTP request in a way that a client prefers. The Accept header of an HTTP request informs the server which MIME types the client is expecting back from this request, with weighting to indicate the most prefered. If your server can respond in multiple formats (e.g.: JSON, XML, HTML), the client can easily tell your server which is the prefered format without resorting to hacks like ‘&format=json’ on the end of query strings.



When provided with an Accept header and a list of types your server can respond with, this function returns the clients most prefered type. This function will only return one of the acceptable types you passed in, or None if no suitable type was found:

from accept_type import get_best_match

def get_the_info(request):
        info = gather_info()

        return_type = get_best_match(request.META.get('HTTP_ACCEPT'), ['text/html', 'application/xml', 'text/json'])

        if return_type == 'application/xml':
                return render_xml(info)

        elif return_type == 'text/json':
                return render_json(info)

        elif return_type == 'text/html':
                return render_html(info)

        elif return_type == None:
                return HttpResponse406()


When provided with an Accept header, this will parse it and return a sorted list of the clients accepted mime types. These will be instances of the AcceptableType class.

>>> from accept_type import parse_header
>>> parse_header('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
['text/html, weight 1', 'application/xhtml+xml, weight 1', 'application/xml, weight 0.9', '*/*, weight 0.8']


AcceptableType instances represent one of the types that a client is willing to accept. This type could include wildcards, to match more than one MIME type.

>>> from accept_type import AcceptableType
>>> type = AcceptableType('image/*;q=0.9')
>>> type.mime_type
>>> type.weight
>>> type.matches('image/png')
>>> type.matches('text/html')

Release History

This version
History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(3.0 kB) Copy SHA256 Hash SHA256
Source None Oct 12, 2015

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting