Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'resulting': 0.04; 'encoding': 0.05; 'output': 0.05; 'subsequent': 0.05; 'binary': 0.07; 'error:': 0.07; 'nicely': 0.07; 'python3': 0.07; 'sys': 0.07; 'utf-8': 0.07; 'locale': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:script': 0.09; 'python': 0.11; 'apache': 0.15; '"r")': 0.16; 'codec': 0.16; 'index.html': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Unicode': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'solution.': 0.20; 'input': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'byte': 0.24; 'skip:l 30': 0.24; 'typical': 0.24; 'looks': 0.24; "i've": 0.25; 'script': 0.25; 'pending': 0.26; 'header:X-Complaints-To:1': 0.27; 'gives': 0.31; 'serve': 0.31; "skip:' 10": 0.31; 'prints': 0.31; 'anyone': 0.31; 'file': 0.32; 'me?': 0.32; 'run': 0.32; 'skip:# 10': 0.33; 'could': 0.34; 'knowledge': 0.35; 'subject:with': 0.35; "can't": 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'accessing': 0.36; 'done': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'skip:o 30': 0.61; 'show': 0.63; 'choose': 0.64; 'more': 0.64; 'webpage': 0.68; 'default': 0.69; 'jul': 0.74; '1997': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Unicode in cgi-script with apache2 Date: Sat, 16 Aug 2014 13:17:16 +0200 Organization: None References: <53EE4D11.7040604@ramaekers-stassart.be> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8add.dip0.t-ipconnect.de User-Agent: KNode/4.13.2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408187854 news.xs4all.nl 2871 [2001:888:2000:d::a6]:40953 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76404 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.