For HTML5 and Python CGI:
If I write UTF-8 Meta Tag, my code doesn't work. If I don't write, it works.
Page encoding is UTF-8.
print("Content-type:text/html")
print()
print("""
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
şöğıçü
</body>
</html>
""")
This codes doesn't work.
print("Content-type:text/html")
print()
print("""
<!doctype html>
<html>
<head></head>
<body>
şöğıçü
</body>
</html>
""")
But this codes works.
From https://ru.stackoverflow.com/a/352838/11350
First dont forget to set encoding in file
Then try
Or if you use apache2, add to your conf.
For CGI, using
print()
requires that the correct codec has been set up for output.print()
writes tosys.stdout
andsys.stdout
has been opened with a specific encoding and how that is determined is platform dependent and can differ based on how the script is run. Running your script as a CGI script means you pretty much do not know what encoding will be used.In your case, the web server has set the locale for text output to a fixed encoding other than UTF-8. Python uses that locale setting to produce output in in that encoding, and without the
<meta>
header your browser correctly guesses that encoding (or the server has communicated it in the Content-Type header), but with the<meta>
header you are telling it to use a different encoding, one that is incorrect for the data produced.You can write directly to
sys.stdout.buffer
, after explicitly encoding to UTF-8. Make a helper function to make this easier:Another approach is to replace
sys.stdout
with a newio.TextIOWrapper()
object that uses the codec you need: