what is wrong in my code?? (python 3.3)

D

dream4soul

#!c:/Python33/python.exe -u

import os, sys
print("Content-type: text/html; charset=utf-8\n\n")


print ('Hello, world!<hr>')

print('ранее предуÑматривалаÑÑŒ ÑÐ¼ÐµÑ€Ñ‚Ð½Ð°Ñ ÐºÐ°Ð·Ð½ÑŒ.')


I see only first print, second it just question marks in my browser(code edited in notepad++ with UTF-8 encode).
what is wrong??
 
D

Denis McMahon

#!c:/Python33/python.exe -u
import os, sys
print("Content-type: text/html; charset=utf-8\n\n")
print ('Hello, world!<hr>')
print('ранее предуÑматривалаÑÑŒ ÑÐ¼ÐµÑ€Ñ‚Ð½Ð°Ñ ÐºÐ°Ð·Ð½ÑŒ.')
I see only first print, second it just question marks in my browser(code
edited in notepad++ with UTF-8 encode). what is wrong??

Sounds like your browser is ignoring the charset. Can you force the
browser to utf-8?

What happens if you create a plain html file with the same content and
send it to your browser?

eg: test.html:
 
D

dream4soul

Sounds like your browser is ignoring the charset. Can you force the

browser to utf-8?



What happens if you create a plain html file with the same content and

send it to your browser?



eg: test.html:

-----------------

Hello, world!<hr>

ранее предуÑматривалаÑÑŒ ÑÐ¼ÐµÑ€Ñ‚Ð½Ð°Ñ ÐºÐ°Ð·Ð½ÑŒ.

-----------------



This really doesn't look like a python issue (again).

I rename file from test.py in test.txt and all works fine. So clearly problem it is not in file coding or browser. ANY IDEAS??
 
P

Piet van Oostrum

I rename file from test.py in test.txt and all works fine. So clearly problem it is not in file coding or browser. ANY IDEAS??

It looks like the encoding of stdout is not utf-8 in the CGI script. Check it with

import sys
print(sys.stdout.encoding)

If it's not utf-8, you must force your output to be utf-8, as that is what the browser expects, because of your Content-type.

You could use:
sys.stdout.buffer.write('ранее предуÑматривалаÑÑŒ ÑÐ¼ÐµÑ€Ñ‚Ð½Ð°Ñ ÐºÐ°Ð·Ð½ÑŒ.'.encode('utf-8'))

Or to change stdout into utf-8 encoding:

import codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

[Note: I haven't tested this]
 
D

dream4soul

COOL!!!

print(sys.stdout.encoding)

show cp1251


sys.stdout.buffer.write('ранее предуÑматривалаÑÑŒ ÑÐ¼ÐµÑ€Ñ‚Ð½Ð°Ñ ÐºÐ°Ð·Ð½ÑŒ.'.encode('utf-8'))

works fine!!!

sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

this fix the print problem!!!

Big Thank Piet van Oostrum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,529
Members
45,001
Latest member
Kendra00E1

Latest Threads

Top