簡體   English   中英

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

[英]Returning a different mime-type in Python Bottle

我有一個簡單的Python Bottle應用程序,主要返回HTML頁面。

但是一個選項需要返回純文本(MIME類型的“文本/文本”)

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

如何告訴函數返回此類型?

根據文檔

更改默認編碼

Bottle使用Content-Type標頭的charset參數來決定如何編碼unicode字符串。 此標頭默認為text/html; charset=UTF8 text/html; charset=UTF8 ,可以使用Response.content_type屬性或直接設置Response.charset屬性進行更改。 Response對象在“響應對象 ”部分中進行了描述。)

 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' 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM