Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

accept-types

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

accept-types

Determine the best content to send in an HTTP response

  • 0.4.1
  • PyPI
  • Socket score

Maintainers
1

======================================================== accept-types - Use the correct accept type for a request

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.

Usage

get_best_match

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:

.. code:: python

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()

parse_header

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.

.. code:: python

>>> 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

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.

.. code:: python

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

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc