<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Forum Bauen und Umwelt - Umlaute und Unicode-Zeichen in Python-CGI-Skripten</title>
<link>https://bauforum.wirklichewelt.de/</link>
<description>Das Bauforum</description>
<language>de</language>
<item>
<title>Umlaute und Unicode-Zeichen in Python-CGI-Skripten</title>
<content:encoded><![CDATA[<p>Im Februar hatte ich hier einen Trick vorgestellt, mit dem Python-Programme, die als CGI-Skript auf Webservern laufen, dazu gebracht werden können, beliebige Unicode-Zeichen auszugeben. Realisiert wurde das durch eine Neudefinition der print-Funktion, die danach alle nicht-ASCII-Zeichen in ihre ASCII-kompatible XML-Ersatzdarstellung umwandelt. (<a href="index.php?id=11588" class="internal">Python auf dem Webserver: Umlaute und Sonderzeichen trotz Shared Hosting</a>)</p>
<p>Es geht jedoch noch einfacher. Das Problem ist ja nicht, dass die Server der Shared-Hosting-Pakete nicht Unicode-tauglich wären, sondern dass Python das lediglich nicht zweifelsfrei herausfinden kann, weil beispielsweise bei 1und1 die entsprechende Umgebungsvariable nicht gesetzt wird.</p>
<p>Konfiguriert man in einem Python-Modul die Ein- und Ausgaberoutinen von Python so um, dass sie stets Unicode verwenden, so lassen sich CGI-Skripte ganz einfach dadurch sharedhostertauglich machen, dass man zu Beginn seines CGI-Skriptes dieses Modul importiert. Auf meinem Webauftritt habe ich die folgenden Zeilen als „einsundeins.py“ abgelegt und alle CGI-Skripte dort enthalten die Zeile „import einsundeins“.</p>
<pre class="python" style="font-family:monospace;"><ol><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;">#!/usr/bin/env python3</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># Biegt die Ausgabekodierung für den Betrieb auf 1&amp;1-Webservern zurecht.</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">import</span> <span style="color: #dc143c;">sys</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">import</span> <span style="color: #dc143c;">codecs</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">if</span> <span style="color: #080;">&quot;idlelib&quot;</span> <span style="color: #ff7700;">in</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">modules</span>:</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #808;">print</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;Modul 'einsundeins' wäre jetzt aktiv, wenn nicht IDLE liefe.&quot;</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">else</span>:</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #ff7700;">try</span>:</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span> <span style="color: #000;">=</span> <span style="color: #dc143c;">codecs</span>.<span style="color: black;">getreader</span><span style="color: black;">&#40;</span><span style="color: #080;">'utf8'</span><span style="color: black;">&#41;</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>.<span style="color: black;">buffer</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span> <span style="color: #000;">=</span> <span style="color: #dc143c;">codecs</span>.<span style="color: black;">getwriter</span><span style="color: black;">&#40;</span><span style="color: #080;">'utf8'</span><span style="color: black;">&#41;</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">buffer</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #ff7700;">except</span> <span style="color: #808;">AttributeError</span>:</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #808;">print</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;Content-Type: text/html;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #808;">print</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;UTF-8-Umschaltung fehlgeschlagen!&lt;br/&gt;&quot;</span><span style="color: black;">&#41;</span></div></li></ol></pre><p>Für Testzwecke ist hier noch eine Fallunterscheidung eingebaut, die dafür sorgt, dass  das Modul erkennt, ob gerade die Entwicklungsumgebung IDLE läuft, da diese ebenfalls die Ein- und Ausgaben umleitet. Wer das nicht benötigt, kann die drei Zeilen von „if“ bis „else:“ löschen und die Einrückung dahinter entfernen.</p>
]]></content:encoded>
<link>https://bauforum.wirklichewelt.de/index.php?id=11668</link>
<guid>https://bauforum.wirklichewelt.de/index.php?id=11668</guid>
<pubDate>Tue, 18 Sep 2018 09:45:03 +0000</pubDate>
<category>Software</category><dc:creator>Martin Vogel</dc:creator>
</item>
</channel>
</rss>
