import webob from paste.httpserver import serve from paste.urlmap import URLMap import cgi import urllib word = u'fran\xe7ais' def encoding_app(environ, start_response): body = u''' UTF8 app
encoding=ENCODING
SCRIPT_NAME=%(SCRIPT_NAME)s
PATH_INFO=%(PATH_INFO)s
QUERY_STRING=%(QUERY_STRING)s
view in latin1 | view in utf8
example latin1: LATIN1_RAW | example utf8: UTF8_RAW
The link: %(latin1)s
In UTF8: %(utf8)s
Unencoded: %(raw)s (latin1, utf8)
Form:
With encoding:
''' % dict( PATH_INFO=cgi.escape(repr(environ['PATH_INFO'])), SCRIPT_NAME=cgi.escape(repr(environ['SCRIPT_NAME'])), QUERY_STRING=cgi.escape(repr(environ['QUERY_STRING'])), latin1=urllib.quote(word.encode('latin1')), utf8=urllib.quote(word.encode('utf8')), raw=word) if 'latin1' in environ.get('QUERY_STRING', ''): encoding = 'latin1' else: encoding = 'utf8' body = body.encode(encoding) body = body.replace('LATIN1_RAW', word.encode('latin1')) body = body.replace('UTF8_RAW', word.encode('utf8')) body = body.replace('ENCODING', encoding) start_response('200 OK', [('Content-Type', 'text/html; charset=%s' % encoding)]) return [body] if __name__ == '__main__': serve(encoding_app)