[apache python3 cgi] problème accent

bonjour

J’ai un problème avec les accents

voici un ecm qui fonctionne

[code]#!/usr/bin/python3

-∗- coding: utf-8 -∗-

print(“Content-Type: text/html\n”) # préciser dans l’entête HTTP le format du document
print(‘

c’est bon

’)
texte='

un accent é


print(‘texte’)[/code]

mais si je met é à la é rien n’est affiché.

J’ai certe contourné le problème avec

Je voudrais savoir si cela est normal.
Je sais bien que é est traduit par é en html mais quand j’écris un texte directement avec mon éditeur favoris et que je met é j’obtient bien la page.

Pour information avec cgi.test() j’obtiens:

[quote]Current Working Directory:

/home/limax/Documents/apache/python
Command Line Arguments:

[’/home/limax/Documents/apache/python/pyinfo.py’]
Form Contents:

No form fields.

Shell Environment:

DOCUMENT_ROOT
/home/limax/Documents/apache
GATEWAY_INTERFACE
CGI/1.1
HTTP_ACCEPT
text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
HTTP_ACCEPT_CHARSET
ISO-8859-1,utf-8;q=0.7,*;q=0.3
HTTP_ACCEPT_ENCODING
gzip,deflate,sdch
HTTP_ACCEPT_LANGUAGE
fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
HTTP_CACHE_CONTROL
max-age=0
HTTP_CONNECTION
keep-alive
HTTP_HOST
localhost
HTTP_USER_AGENT
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
PATH
/usr/local/bin:/usr/bin:/bin
QUERY_STRING
REMOTE_ADDR
::1
REMOTE_PORT
41183
REQUEST_METHOD
GET
REQUEST_URI
/python/pyinfo.py
SCRIPT_FILENAME
/home/limax/Documents/apache/python/pyinfo.py
SCRIPT_NAME
/python/pyinfo.py
SERVER_ADDR
::1
SERVER_ADMIN
webmaster@localhost
SERVER_NAME
localhost
SERVER_PORT
80
SERVER_PROTOCOL
HTTP/1.1
SERVER_SIGNATURE

Apache/2.2.22 (Debian) Server at localhost Port 80 SERVER_SOFTWARE Apache/2.2.22 (Debian) These environment variables could have been set:

AUTH_TYPE
CONTENT_LENGTH
CONTENT_TYPE
DATE_GMT
DATE_LOCAL
DOCUMENT_NAME
DOCUMENT_ROOT
DOCUMENT_URI
GATEWAY_INTERFACE
LAST_MODIFIED
PATH
PATH_INFO
PATH_TRANSLATED
QUERY_STRING
REMOTE_ADDR
REMOTE_HOST
REMOTE_IDENT
REMOTE_USER
REQUEST_METHOD
SCRIPT_NAME
SERVER_NAME
SERVER_PORT
SERVER_PROTOCOL
SERVER_ROOT
SERVER_SOFTWARE
In addition, HTTP headers sent by the server may be passed in the environment as well. Here are some common variable names:
HTTP_ACCEPT
HTTP_CONNECTION
HTTP_HOST
HTTP_PRAGMA
HTTP_REFERER
HTTP_USER_AGENT
What follows is a test, not an actual exception:

Traceback (most recent call last):

File “/usr/lib/python3.2/cgi.py”, line 879, in test
g()
File “/usr/lib/python3.2/cgi.py”, line 877, in g
f()
File “/usr/lib/python3.2/cgi.py”, line 875, in f
exec(“testing print_exception() – italics?”)
File “”, line 1
testing print_exception() – italics?
^
SyntaxError: invalid syntax
Second try with a small maxlen…

Current Working Directory:

/home/limax/Documents/apache/python
Command Line Arguments:

[’/home/limax/Documents/apache/python/pyinfo.py’]
Form Contents:

No form fields.

Shell Environment:

DOCUMENT_ROOT
/home/limax/Documents/apache
GATEWAY_INTERFACE
CGI/1.1
HTTP_ACCEPT
text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
HTTP_ACCEPT_CHARSET
ISO-8859-1,utf-8;q=0.7,*;q=0.3
HTTP_ACCEPT_ENCODING
gzip,deflate,sdch
HTTP_ACCEPT_LANGUAGE
fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
HTTP_CACHE_CONTROL
max-age=0
HTTP_CONNECTION
keep-alive
HTTP_HOST
localhost
HTTP_USER_AGENT
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
PATH
/usr/local/bin:/usr/bin:/bin
QUERY_STRING
REMOTE_ADDR
::1
REMOTE_PORT
41183
REQUEST_METHOD
GET
REQUEST_URI
/python/pyinfo.py
SCRIPT_FILENAME
/home/limax/Documents/apache/python/pyinfo.py
SCRIPT_NAME
/python/pyinfo.py
SERVER_ADDR
::1
SERVER_ADMIN
webmaster@localhost
SERVER_NAME
localhost
SERVER_PORT
80
SERVER_PROTOCOL
HTTP/1.1
SERVER_SIGNATURE

Apache/2.2.22 (Debian) Server at localhost Port 80 SERVER_SOFTWARE Apache/2.2.22 (Debian)[/quote]

J’ai trouvé une autre possibilité pour contourner ce problème mais j’aimerai bien comprendre, donc si quelqu’un a une explication à me donner.

l’ecm ci-dessous fonctionne trouvé ici

[code]import locale
import sys

locale.getpreferredencoding = lambda: ‘UTF-8’ # are UTF-8 encoded.
sys.stdin = open(’/dev/stdin’, ‘r’) # Re-open standard files in UTF-8
sys.stdout = open(’/dev/stdout’, ‘w’) # mode.
sys.stderr = open(’/dev/stderr’, ‘w’)

print(“Content-Type: text/html ;charset=utf-8\n”) # préciser dans l’entête HTTP le format du document

print(‘

c’est bon

’)
print(‘

é

’)[/code]

Je vais essayé de comprendre

Pour tes tests je te conseil d’utiliser curl avec l’option -vvv. Ça te permettra de voir l’ensemble des requêtes HTTP et leur réponse.

Quand on regarde pour www.google.fr

[code]> GET http://www.google.fr HTTP/1.1

User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
Host: www.google.fr
Accept: /
Proxy-Connection: Keep-Alive

  • HTTP 1.0, assume close after body
    < HTTP/1.0 200 OK
    < Date: Thu, 31 Jan 2013 12:09:52 GMT
    < Expires: -1
    < Cache-Control: private, max-age=0
    < Content-Type: text/html; charset=ISO-8859-1
    < P3P: CP=“This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info.”
    < Server: gws
    < X-XSS-Protection: 1; mode=block
    < X-Frame-Options: SAMEORIGIN
    < Age: 0
    < Via: 1.1 localhost.localdomain
    < Content-Length: 13733
    < Set-Cookie: PREF=ID=7a3017fdf52d02c6:FF=0:TM=1359634192:LM=1359634192:S=bCHgMzv3bLG_pkw4; expires=Sat, 31-Jan-2015 12:09:52 GMT; path=/; domain=.google.fr
    < X-Cache: MISS from ecfrec.frec.bull.fr
  • HTTP/1.0 proxy connection set to keep alive!
    < Proxy-Connection: keep-alive[/code]

Tu vois dans le Content-Type :

Je pense que c’est ça que tu modifie avec :

[quote=“MisterFreez”]Pour tes tests je te conseil d’utiliser curl avec l’option -vvv. Ça te permettra de voir l’ensemble des requêtes HTTP et leur réponse.

Je pense que c’est ça que tu modifie avec :

bizarre quand même

j’essaye avec ceci

[code]
print(“Content-Type: text/html ;charset=utf-8\n”) # préciser dans l’entête HTTP le format du document

print(‘

c’est bon

’)
print(‘

é

’)[/code]
j’obtiens bien

[code]curl -vvv http://localhost/python/test2.py

  • About to connect() to localhost port 80 (#0)
  • Trying ::1…
  • connected
  • Connected to localhost (::1) port 80 (#0)

GET /python/test2.py HTTP/1.1
User-Agent: curl/7.28.0
Host: localhost
Accept: /

< HTTP/1.1 200 OK
< Date: Thu, 31 Jan 2013 12:59:32 GMT
< Server: Apache/2.2.22 (Debian)
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
< Content-Type: text/html ;charset=utf-8
<

c'est bon

* Connection #0 to host localhost left intact * Closing connection #0[/code] Le charset est bien utf-8 et je n'ai pas le é.

Je vais quand même mettre résolu.

Tu obtiens quoi comme requête avec getpreferredencoding ?

effectivement mon pbm est ici

avec ipython j’obtiens bien locale.getpreferredencoding() Out[3]: 'UTF-8'

mais dans un script apache+python cgi j’obtiens ANSI_X3.4-1968

je vais donc chercher dans cette voie, 'je pense pour un pbm de configuration d’apache)

je ne met pas encore resolu

toujours rien

j’ai modifié /etc/apache2/envvars pour mettre

export LANG='fr_FR.UTF-8' export LC_ALL='fr_FR.UTF-8'

comme indiqué ici

c’est toujours pareil grrrrrrrrr :075 :075 :075 :075

j’obtiens avec phpinfo() ceci : Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.3

est ce que vous savez comment changer ceci, par exemple en: Accept-Charset ISO-8859-1,utf-8;q=0.3,*;q=0.7

mon poste n’est plus tout à fait dans la bonne catégorie. je vais chercher un peu et si je ne trouve pas je posterai dans une autre catégorie.

Je manque de visibilité sur ce que tu fais. C’est un script CGI en python exécuté par apache c’est ça ?

phpinfo() sort des informations d’apache mais aussi de php (qui sont dans le php.ini) donc je ne suis pas sûr de savoir quelle configuration viens d’où.

oui c’est exactement ca

à force de rechercher je comprends de moins en moins :030
je commence à penser qu’il s’agit d’un bug (ou alors il faut utiliser une autre méthode avec python3 mais je ne trouve pas).
je ne suis pas seul à avoir ce problème donc je continue de chercher.

merci encore de me donner un coup de main.