Error de codificación de texto en CGI
Sergio Martín
sergiomartinj en gmail.com
Dom Oct 18 10:28:03 CEST 2009
Hola.
Estoy intentando hacer una web mediante Python en CGI, utilizo el set
de caracteres UTF-8, toda va bien hasta que meto alguna tilde, pongo
un código de ejemplo:
#! /usr/local/bin/python3
# -*- coding: utf_8 -*-
import cgitb
cgitb.enable()
print("Content-Type: text/html; charset=UTF-8")
print()
print("Linea sin tilde") # Se ve correctamente
print("Línea con tilde") # No se ve
Uso python 3.1.1 sobre Mac OS X 10.6.1, el fichero está codificado
como UTF-8, no BOM, y tiene permiso de ejecución.
En el navegador (Safari 4.0.3) solo se ve la primera linea, y si le
doy a "Ver código fuente" sale:
Linea sin tilde
<!--: spam
Content-Type: text/html
<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> -->
<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --
> -->
</font> </font> </font> </script> </object> </blockquote> </
pre>
</table> </table> </table> </table> </table> </font> </
font> </font>
El log del Apache me muestra:
[Sun Oct 18 10:20:54 2009] [error] [client
fe80::217:f2ff:fee7:9901] Error in sys.excepthook:
[Sun Oct 18 10:20:54 2009] [error] [client
fe80::217:f2ff:fee7:9901] UnicodeEncodeError: 'ascii' codec can't
encode character '\\xed' in position 1601: ordinal not in range(128)
[Sun Oct 18 10:20:54 2009] [error] [client
fe80::217:f2ff:fee7:9901]
[Sun Oct 18 10:20:54 2009] [error] [client
fe80::217:f2ff:fee7:9901] Original exception was:
[Sun Oct 18 10:20:54 2009] [error] [client
fe80::217:f2ff:fee7:9901] Traceback (most recent call last):
[Sun Oct 18 10:20:54 2009] [error] [client
fe80::217:f2ff:fee7:9901] File "/Users/sergio/Sites/python/
prueba.py", line 10, in <module>
[Sun Oct 18 10:20:54 2009] [error] [client
fe80::217:f2ff:fee7:9901] print("L\\xednea con tilde") # No se ve
[Sun Oct 18 10:20:54 2009] [error] [client
fe80::217:f2ff:fee7:9901] UnicodeEncodeError: 'ascii' codec can't
encode character '\\xed' in position 1: ordinal not in range(128)
Si ejecuto el script desde la línea de comandos, el resultado es el
correcto, con tildes.
Y sin usar Python, en un fichero html el resultado también es correcto.
¿Alguna sugerencia?
Gracias._______________________________________________
Lista de correo Python-es
http://listas.aditel.org/listinfo/python-es
FAQ: http://listas.aditel.org/faqpyes
Más información sobre la lista de distribución Python-es