Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76405

Re: Unicode in cgi-script with apache2

X-Received by 10.182.45.162 with SMTP id o2mr12135733obm.20.1408197735429; Sat, 16 Aug 2014 07:02:15 -0700 (PDT)
X-Received by 10.140.23.163 with SMTP id 32mr37974qgp.8.1408197735403; Sat, 16 Aug 2014 07:02:15 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!uq10no588250igb.0!news-out.google.com!j6ni190qas.0!nntp.google.com!m5no344307qaj.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Sat, 16 Aug 2014 07:02:15 -0700 (PDT)
In-Reply-To <mailman.13050.1408187854.18130.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=2a02:120b:c3cc:5710:7893:4260:f33e:418b; posting-account=ung4FAoAAAC46zhHJ0Nsnuox7M5gDvs_
NNTP-Posting-Host 2a02:120b:c3cc:5710:7893:4260:f33e:418b
References <53EE4D11.7040604@ramaekers-stassart.be> <mailman.13050.1408187854.18130.python-list@python.org>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <cbca55df-3990-40e2-9487-3d85471214ca@googlegroups.com> (permalink)
Subject Re: Unicode in cgi-script with apache2
From wxjmfauth@gmail.com
Injection-Date Sat, 16 Aug 2014 14:02:15 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.python:76405

Show key headers only | View raw


Le samedi 16 août 2014 13:17:16 UTC+2, Peter Otten a écrit :
> Dominique Ramaekers wrote:
> 
> 
> 
> > I've got a little script:
> 
> > 
> 
> > #!/usr/bin/env python3
> 
> > print("Content-Type: text/html")
> 
> > print("Cache-Control: no-cache, must-revalidate")    # HTTP/1.1
> 
> > print("Expires: Sat, 26 Jul 1997 05:00:00 GMT") # Date in the past
> 
> > print("")
> 
> > f = open("/var/www/cgi-data/index.html", "r")
> 
> > for line in f:
> 
> >      print(line,end='')
> 
> > 
> 
> > If I run the script in the terminal, it nicely prints the webpage
> 
> > 'index.html'.
> 
> > 
> 
> > If access the script through a webbrowser, apache gives an error:
> 
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
> 
> > 1791: ordinal not in range(128)
> 
> > 
> 
> > I've done a hole afternoon of reading on fora and blogs, I don't have a
> 
> > solution.
> 
> > 
> 
> > Can anyone help me?
> 
> 
> 
> If the input and output encoding are the same you can avoid the byte-to-text 
> 
> (and subsequent text-to-byte conversion) and serve the binary contents of 
> 
> the index.html file directly:
> 
> 
> 
> #!/usr/bin/env python3
> 
> import sys
> 
> 
> 
> print("Content-Type: text/html")
> 
> print("Cache-Control: no-cache, must-revalidate")    # HTTP/1.1
> 
> print("Expires: Sat, 26 Jul 1997 05:00:00 GMT") # Date in the past
> 
> print("")
> 
> sys.stdout.flush()
> 
> with open("/var/www/cgi-data/index.html", "rb") as f:
> 
>     for line in f:
> 
>         sys.stdout.buffer.write(line)
> 
> 
> 
> The flush() is necessary to write pending data before accessing the lowlevel 
> 
> stdout.buffer. Instead of the loop you can use any of these:
> 
> 
> 
> sys.stdout.buffer.write(f.read()) # not for huge files, but should be OK for
> 
>                                   # typical html file sizes
> 
> sys.stdout.buffer.writelines(f)
> 
> shutil.copyfileobj(f, sys.stdout.buffer) # show off your knowledge 
> 
>                                          # of the stdlib ;)
> 
> 
> 
> 
> 
> Alternatively you could choose an encoding via the locale:
> 
> 
> 
> #!/usr/bin/env python3
> 
> import locale
> 
> locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
> 
> 
> 
> print("Content-Type: text/html")
> 
> print("Cache-Control: no-cache, must-revalidate")    # HTTP/1.1
> 
> print("Expires: Sat, 26 Jul 1997 05:00:00 GMT") # Date in the past
> 
> print("")
> 
> with open("/var/www/cgi-data/index.html") as f:
> 
>     for line in f:
> 
>         print(line, end='')
> 
> 
> 
> Python should then use UTF-8 as the default for i/o and the resulting 
> 
> scripts looks more familiar.

Wrong. It will not work with a Japanese or a Korean iso-2022-xx
html file.

jmf

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Unicode in cgi-script with apache2 Peter Otten <__peter__@web.de> - 2014-08-16 13:17 +0200
  Re: Unicode in cgi-script with apache2 wxjmfauth@gmail.com - 2014-08-16 07:02 -0700

csiph-web