Can't print Chinese to HTTP

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Mon Nov 30 14:32:31 EST 2009


On 05:05 pm, gnarlodious at gmail.com wrote:
>Thanks for the help, but it doesn't work. All I get is an error like:
>
>UnicodeEncodeError: 'ascii' codec can't encode character '\\u0107' in
>position 0: ordinal not in range(128)
>
>It does work in Terminal interactively, after I import the sys module.
>But my script doesn't act the same. Here is my entire script:
>
>#!/usr/bin/python
>print("Content-type:text/plain;charset=utf-8\n\n")
>import sys
>sys.stdout.buffer.write('f49\n'.encode("utf-8"))
>
>All I get is the despised "Internal Server Error" with Console
>reporting:
>
>malformed header from script. Bad header=\xe6\x99\x89

As the error suggests, you're writing f49 to the headers section of the 
response.  This is because you're not ending the headers section with a 
blank line.  Lines in HTTP end with \r\n, not with just \n.

Have you considered using something with fewer sharp corners than CGI? 
You might find it more productive.

Jean-Paul



More information about the Python-list mailing list