How to set the content type header in response for a particular file type in Pyramid web framework

痴心易碎 提交于 2019-12-04 09:37:28

Pyramid uses the standard mimetypes module to guess the mimetype based on the extension. It calls:

mimetypes.guess_type(path, strict=False)

The module looks in the Windows registry if on that platform, and in the following locations for mimetype lists:

knownfiles = [
    "/etc/mime.types",
    "/etc/httpd/mime.types",                    # Mac OS X
    "/etc/httpd/conf/mime.types",               # Apache
    "/etc/apache/mime.types",                   # Apache 1
    "/etc/apache2/mime.types",                  # Apache 2
    "/usr/local/etc/httpd/conf/mime.types",
    "/usr/local/lib/netscape/mime.types",
    "/usr/local/etc/httpd/conf/mime.types",     # Apache 1.2
    "/usr/local/etc/mime.types",                # Apache 1.3
    ]

You can either extend one of those files, or create your own file and add it to the module using the .init() function.

The file format is simple, just list the mimetype, then some whitespace, then a space-separated list of extensions:

application/x-font-woff     woff

Simply add this following code where your Pyramid web app gets initialized.

import mimetypes mimetypes.add_type('application/x-font-woff', '.woff')

For instance, I have added it in my webapp.py file, which gets called the first time the server gets hit with a request.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!