# \[apache python3 cgi\] problème accent

**URL:** <https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466>\
**Category:** Programmation\
**Created:** [Février 21, 2016, 2:08 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466 "2016-02-21T14:08:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![limax](https://www.debian-fr.org/user_avatar/www.debian-fr.org/limax/32/3886_2.png) [@limax](https://www.debian-fr.org/u/limax)\
**Post date:** [Février 21, 2016, 2:08 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/1 "2016-02-21T14:08:45Z")

</div>

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

```auto

```

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]

---

<div class="post-metadata">

**Author:** ![limax](https://www.debian-fr.org/user_avatar/www.debian-fr.org/limax/32/3886_2.png) [@limax](https://www.debian-fr.org/u/limax)\
**Post date:** [Février 21, 2016, 2:08 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/2 "2016-02-21T14:08:45Z")

</div>

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](http://stackoverflow.com/questions/9322410/set-encoding-in-python-3-cgi-scripts)

[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

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 21, 2016, 2:08 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/3 "2016-02-21T14:08:46Z")

</div>

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](http://www.google.fr)

[code]\> GET [http://www.google.fr](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](http://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=«&nbsp;This is not a P3P policy! See [Google Account Help](http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657) for more info.&nbsp;»  
\< 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](http://domain=.google.fr)  
\< X-Cache: MISS from [ecfrec.frec.bull.fr](http://ecfrec.frec.bull.fr)
- HTTP/1.0 proxy connection set to keep alive!  
\< Proxy-Connection: keep-alive[/code]

Tu vois dans le Content-Type :

```auto
Content-Type: text/html; charset=ISO-8859-1
```

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

```auto
locale.getpreferredencoding = lambda: 'UTF-8' # are UTF-8 encoded.
```

---

<div class="post-metadata">

**Author:** ![limax](https://www.debian-fr.org/user_avatar/www.debian-fr.org/limax/32/3886_2.png) [@limax](https://www.debian-fr.org/u/limax)\
**Post date:** [Février 21, 2016, 2:08 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/4 "2016-02-21T14:08:46Z")

</div>

[quote=«&nbsp;MisterFreez&nbsp;»]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.  
…

```auto
Content-Type: text/html; charset=ISO-8859-1
```

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

`locale.getpreferredencoding = lambda: 'UTF-8' # are UTF-8 encoded.`[/quote]  
bizarre quand même

j’essaye avec ceci

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

print(‹&nbsp;

c'est bon

&nbsp;›)  
print(‹&nbsp;

é

&nbsp;›)[/code]  
j’obtiens bien

[code]curl -vvv [http://localhost/python/test2.py](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.

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 21, 2016, 2:08 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/5 "2016-02-21T14:08:47Z")

</div>

Tu obtiens quoi comme requête avec getpreferredencoding ?

---

<div class="post-metadata">

**Author:** ![limax](https://www.debian-fr.org/user_avatar/www.debian-fr.org/limax/32/3886_2.png) [@limax](https://www.debian-fr.org/u/limax)\
**Post date:** [Février 21, 2016, 2:08 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/6 "2016-02-21T14:08:50Z")

</div>

> [@MisterFreez](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![limax](https://www.debian-fr.org/user_avatar/www.debian-fr.org/limax/32/3886_2.png) [@limax](https://www.debian-fr.org/u/limax)\
**Post date:** [Février 21, 2016, 2:09 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/7 "2016-02-21T14:09:01Z")

</div>

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](https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/)

c’est toujours pareil grrrrrrrrr ![:075](https://www.debian-fr.org/uploads/default/original/1X/660d70f9421e915a2561756e6d9f245d4f609347.gif "dequoi") ![:075](https://www.debian-fr.org/uploads/default/original/1X/660d70f9421e915a2561756e6d9f245d4f609347.gif "dequoi") ![:075](https://www.debian-fr.org/uploads/default/original/1X/660d70f9421e915a2561756e6d9f245d4f609347.gif "dequoi") ![:075](https://www.debian-fr.org/uploads/default/original/1X/660d70f9421e915a2561756e6d9f245d4f609347.gif "dequoi")

---

<div class="post-metadata">

**Author:** ![limax](https://www.debian-fr.org/user_avatar/www.debian-fr.org/limax/32/3886_2.png) [@limax](https://www.debian-fr.org/u/limax)\
**Post date:** [Février 21, 2016, 2:09 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/8 "2016-02-21T14:09:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 21, 2016, 2:09 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/9 "2016-02-21T14:09:13Z")

</div>

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ù.

---

<div class="post-metadata">

**Author:** ![limax](https://www.debian-fr.org/user_avatar/www.debian-fr.org/limax/32/3886_2.png) [@limax](https://www.debian-fr.org/u/limax)\
**Post date:** [Février 21, 2016, 2:09 UTC](https://www.debian-fr.org/t/apache-python3-cgi-probleme-accent/56466/10 "2016-02-21T14:09:15Z")

</div>

> [@MisterFreez](#):
>
> Je manque de visibilité sur ce que tu fais. C’est un script CGI en python exécuté par apache c’est ça ?

oui c’est exactement ca

à force de rechercher je comprends de moins en moins ![:030](https://www.debian-fr.org/uploads/default/original/1X/65b1b73704eec538091c0e1d04a633b3840a284f.gif "picole")  
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.
