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

Hello, the rpy below returns a string with special characters, which I would like to be displayed by the web client. However the 3 non-ascii characters below are displayed as garbage (even if differently), in two browsers I have tried with (IE and Safari). What am I missing? 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> </head> <body> 3 sample èçé characters (e+grave, c+cedille, e+aigu) </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() ###

Hi, On Fri, 28 Mar 2003 15:00:46 +0000, Mario Ruggier wrote: This
request.setHeader = ('Content-Type', 'text/html; charset=utf-8')
seems to have a '=' sign in it which doesn't make ANY sense at all.
3 sample èçé characters (e+grave, c+cedille, e+aigu)
Note that your email was encoded as iso-8859-1. -- Matthias

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- èçé -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() ###

On Sat, 29 Mar 2003 06:42:30 +0100 "Matthias Urlichs" <smurf@noris.de> 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.
Exactly! This should be: request.setHeader('Content-Type', 'text/html; charset=utf-8') I assume. -- Itamar Shtull-Trauring http://itamarst.org/ http://www.zoteca.com -- Python & Twisted consulting

Hi, On Fri, 28 Mar 2003 15:00:46 +0000, Mario Ruggier wrote: This
request.setHeader = ('Content-Type', 'text/html; charset=utf-8')
seems to have a '=' sign in it which doesn't make ANY sense at all.
3 sample èçé characters (e+grave, c+cedille, e+aigu)
Note that your email was encoded as iso-8859-1. -- Matthias

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- èçé -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() ###

On Sat, 29 Mar 2003 06:42:30 +0100 "Matthias Urlichs" <smurf@noris.de> 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.
Exactly! This should be: request.setHeader('Content-Type', 'text/html; charset=utf-8') I assume. -- Itamar Shtull-Trauring http://itamarst.org/ http://www.zoteca.com -- Python & Twisted consulting
participants (3)
-
Itamar Shtull-Trauring
-
Mario Ruggier
-
Matthias Urlichs