在 Python Bottle 中返回不同的 mime 类型

Returning a different mime-type in Python Bottle

我有一个简单的 Python Bottle 应用程序,主要 returning HTML 页。

但一个选项需要return纯文本(mime 类型"text/text")

@get('/raw/<id>')
def get_raw(id) :
    return get_data(id)

如何将函数指定为 return 这种类型?

根据 the documentation:

Changing the Default Encoding

Bottle uses the charset parameter of the Content-Type header to decide how to encode unicode strings. This header defaults to text/html; charset=UTF8 and can be changed using the Response.content_type attribute or by setting the Response.charset attribute directly. (The Response object is described in the section The Response Object.)

from bottle import response
@route('/iso')
def get_iso():
    response.charset = 'ISO-8859-15'
    return u'This will be sent with ISO-8859-15 encoding.'

@route('/latin9')
def get_latin():
    response.content_type = 'text/html; charset=latin9'
    return u'ISO-8859-15 is also known as latin9.'

因此,在您的情况下,您只需要:

response.content_type = 'text/text; charset=UTF8'