HTTP Response Content-Type charset

1,280 views
Skip to first unread message

Martin Matejek

unread,
May 3, 2011, 9:30:46 AM5/3/11
to cherrypy-users
Hi,
Is there way how to set charset in HTTP response header? It's no big
deal since most web browsers set correct charset from <meta> (X)HTML
tag, but Firefox and other gecko-based web browsers ignores it and use
charset from HTTP header instead. I need to display non-ASCII strings
and it's annoying to manually re-select character encoding every
reload.

Google pointed me to lib/encoding.py file. There I figured out that
CherryPy webserver sends Content-Type="text/html; charset =
ISO-8859-1" if header detection/parsing fail. As far as I tried I was
able to set my own Content-Type, but not the charset.

Following code returns ISO-8859-1 encoded page, despite explicit uft-8
declaration:
@cherrypy.expose
def index (self):
cherrypy.response.headers['Content-Type'] = "text/html;
charset=utf-8"
return "Moje vznášedlo je plné úhořů [My hovercraft is full of
eels]"

However, when function use Unicode string, webpage is returned with
correct, utf-8 charset. Is this bug or feature? I haven't used Python
for a while so maybe there is some obvious explanation, but I'd like
to know the "Pythonic" way to serve utf-8 web pages with CherryPy.

Iztok Kavkler

unread,
May 3, 2011, 10:28:10 AM5/3/11
to cherryp...@googlegroups.com
Perhaps the encoding of your .py file is wrong. Do you have the line

# -*- coding: utf-8 -*-

at the beginning of your .py file?

Robert Brewer

unread,
May 3, 2011, 12:10:06 PM5/3/11
to cherryp...@googlegroups.com
Martin Matejek wrote:
> Following code returns ISO-8859-1 encoded page, despite explicit uft-8
> declaration:
> @cherrypy.expose
> def index (self):
> cherrypy.response.headers['Content-Type'] = "text/html;
> charset=utf-8"
> return "Moje vznášedlo je plné úhořů [My hovercraft is full of
> eels]"
>
> However, when function use Unicode string, webpage is returned with
> correct, utf-8 charset. Is this bug or feature? I haven't used Python
> for a while so maybe there is some obvious explanation, but I'd like
> to know the "Pythonic" way to serve utf-8 web pages with CherryPy.

The encoding tool will try to convert unicode to str, using the arguments you give it. So you have two options:

1. (Recommended) Emit unicode from all your page handlers, and let the encoding tool encode it. By default, the tool will encode the output as the client prefers it (as declared in the client's "Accept-Charset" request header). The encoding tool will alter the Content-Type response header with the successful charset param.

def index(self):
return u'Moje vzn\xe1\u0161edlo je pln\xe9 \xfaho\u0159\u016f'

1b. Sometimes (especially with HTML forms) you want to force a particular page encoding. Supply tools.encoding.encoding, either in a decorator:

@cherrypy.tools.encoding(encoding='utf-8')
def index(self):
return u'Moje vzn\xe1\u0161edlo je pln\xe9 \xfaho\u0159\u016f'

...or in config:

[/]
tools.encoding.encoding: 'utf-8'

2. Do the encoding yourself, and emit str.

def index(self):
cherrypy.response.headers['Content-Type'] = "text/html; charset=utf-8"
return "Moje vznášedlo je plné úhořů"


Robert Brewer
fuma...@aminus.org

Reply all
Reply to author
Forward
0 new messages