Unicode in cgi-script with apache2

alister alister.nospam.ware at ntlworld.com
Fri Aug 15 15:27:20 EDT 2014


On Fri, 15 Aug 2014 20:10:25 +0200, Dominique Ramaekers wrote:

> Hi,
> 
> 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?
> 
> Greetings,
> 
> Dominique.

1) this is not the way to get python to generate a web page, if you dont 
want to use an existing framework (for example if you are doing this ans 
an educational exercise) i suggest to google SWGI

2) you need to encode your output strings  into a format apache/html 
protocols can support - UTF8 is probably best here.
change your pint function to
print(line.encode('utf'),end='') 


3) Ignore any subsequent advice from JMF even when he is trying to help 
he is invariable wrong.
 

-- 
Freedom's just another word for nothing left to lose.
		-- Kris Kristofferson, "Me and Bobby McGee"



More information about the Python-list mailing list