[Twisted-Python] response with special characters, utf-8

Mario Ruggier mario at ruggier.org
Sat Mar 29 05:41:00 EST 2003


On samedi, mars 29, 2003, at 06:42 Europe/Amsterdam, Matthias Urlichs 
wrote:
>> request.setHeader = ('Content-Type', 'text/html; charset=utf-8')
>
> seems to have a '=' sign in it which doesn't make ANY sense at all.

Yeah, looks bizarre. But that's how they say it should be set:
http://www.w3.org/International/O-HTTP-charset

>>              3 sample èçé characters (e+grave, c+cedille, e+aigu)
>
> Note that your email was encoded as iso-8859-1.

Yes, what can I say -- that's mail...

If I try to manually tell python that a string is latin-1, and i want it
in utf-8, i get similar garbage results in the client browsers:

	return unicode('èçé','latin-1').encode('utf-8')

How can I verify that the web clients are indeed receiving utf-8?
It seems that they are, but something very strange is happening
here, on Mac OS Jaguar (note that I am running with
sys.setdefaultencoding('utf-8') in sitecustomize.py):

- in IE:mac 5.2, the character set chosen by default is indeed
UTF-8, but the 3 non-ascii characters are just not displayed,
and IE does not allow to dynamically change the automatically
selected character set

- in Safari (beta 6) the indicated character set is "default", whatever 
that is,
where the 3 chars are not displayed. iIf I change to utf-8 (he allows 
it),
the 3 chars are still not displayed, but if i choose the char set he 
calls
"Western (Mac OS Roman)" the 3 chars are displayed correctly.

Anyone can tell what's going on?
Also, can anyone tell me if the rpy below works correctly on some
python/twistedweb/client configuration ?

Thanks for any help,

	mario


##################################
## chars_test.rpy

from twisted.web import resource
from twisted.web.server import NOT_DONE_YET

def errBack(err, request):
     request.write('error: ' + str(err) )
     request.finish()

def returnString(result, request):
     request.setHeader = ('Content-Type', 'text/html; charset=utf-8')
     request.write(result)
     request.finish()

def responseString(request):
     return '''<html><title>characters test</title>
         <meta http-equiv="Content-Type" content="text/html; 
charset=utf-8">
         </head>
         <body>
             -a- 3 sample chars -b- èçé -c- &egrave;&ccedil;&eacute; -d-
         </body>
         </html>
         '''

class MyResource(resource.Resource):
     def render(self, request):
         from twisted.internet import threads
         d = threads.deferToThread(responseString,request)
         d.addCallback(returnString,request)
         d.addErrback(errBack, request)
         return NOT_DONE_YET

###

resource = MyResource()

###




More information about the Twisted-Python mailing list