Project

General

Profile

[Solved] no German Umlaute with python3 - page stops loading when there is an Umlaut

Added by juliuspeters over 9 years ago

I am using lighttpd/1.4.33 (ssl) (Build-Date: Jan 28 2014 17:26:04) with ubuntu studio 14.02 (3.16.0-49-lowlatency) and python3 (3.4.0). Everything works fine but there is a strange behaviour with German Umlaute (UTF-8 -problem?). A soon as there is a German Umlaut in the web-page stops loading.
When running the scrip at cmd there is no problem with Umlaute. But when I use the same script as a webpage it stops loading when there is a Umlaut.
I did a lot of tests with " charset=utf-8" etc. but there is always the same problem. Don't know what to do ... Can anybody help?

Thank you
Julius

Simple example

#!/usr/bin/env python3
# This Python file uses the following encoding: utf-8
# -*- coding: utf-8 -*-
print("""<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
""")
print("Marz")
print("März")
print("""</body></html>""")

On the cmd there is (skipping html) - as expected:
Marz
März

As a web page there is just "Marz" and nothing following Marz.

locale are
LANG=de_DE.UTF-8
LANGUAGE=de_DE
LC_CTYPE="de_DE.UTF-8" 
LC_NUMERIC="de_DE.UTF-8" 
LC_TIME="de_DE.UTF-8" 
LC_COLLATE="de_DE.UTF-8" 
LC_MONETARY="de_DE.UTF-8" 
LC_MESSAGES="de_DE.UTF-8" 
LC_PAPER="de_DE.UTF-8" 
LC_NAME="de_DE.UTF-8" 
LC_ADDRESS="de_DE.UTF-8" 
LC_TELEPHONE="de_DE.UTF-8" 
LC_MEASUREMENT="de_DE.UTF-8" 
LC_IDENTIFICATION="de_DE.UTF-8" 
LC_ALL=

Replies (2)

RE: no German Umlaute with python3 - page stops loading when there is an Umlaut - Added by stbuehler over 9 years ago

lighttpd (and any other webserver) doesn't care about the binary content.

But the locale is probably not present in the cgi environment; have a look at Docs_ModSetEnv to set the locale in the environment for CGI scripts - my guess is python throws an exception when trying to print some umlaut in the C locale.

Although it is probably a better idea to set utf-8 encoding on the output explicitly instead of relying on the locale from the environment (maybe http://stackoverflow.com/questions/4374455/how-to-set-sys-stdout-encoding-in-python-3 helps)

RE: no German Umlaute with python3 - page stops loading when there is an Umlaut - Added by juliuspeters over 9 years ago

Thank you for your help.

The exception python3 throws is a ValueError "'ascii' codec can't encode characters in position 1: ordinal not in range(128)".

Locale seems not to be present in the cgi environment and probably your first suggestion would be the solution.

Unfortunately I did not get it how to do this. Could you please be more specific.

Thanks
Julius

    (1-2/2)