Can't print Chinese to HTTP

Lie Ryan lie.1296 at gmail.com
Tue Dec 1 10:36:09 EST 2009


On 12/2/2009 12:27 AM, Gnarlodious wrote:
> On Nov 30, 5:53 am, "Martin v. Löwis" wrote:
>
>> #!/usr/bin/python
>> print("Content-type:text/plain;charset=utf-8\n\n")
>> sys.stdout.buffer.write('晉\n'.encode("utf-8"))
>
> Does this work for anyone? Because all I get is a blank page. Nothing.
> If I can establish what SHOULD work, maybe I can diagnose this
> problem.
>

with a minor fix (import sys) that runs without errors in Python 3.1 
(Vista), but the result is a bit disturbing...

--------------------------
晉
Content-type:text/plain;charset=utf-8
<BLANKLINE>
<BLANKLINE>
--------------------------

(is this a bug? or just undefined behavior?)



the following works correctly in python 3.1:

---------------------------
#!/usr/bin/python
import sys
print = lambda s: sys.stdout.buffer.write(s.encode('utf-8'))
print("Content-type:text/plain;charset=utf-8\n\n")
print('晉\n')
----------------------------

(and that code will definitely fail with python2 because of the print 
assignment, an insurance if your server happens to be misconfigured to 
run python2)



More information about the Python-list mailing list