Why python-cgi fails on unicode?

Viewed 5346

If run this code in console - it works well (it is in Russian), but if run it like cgi on Apache2 server - it fails: <type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode characters in position 8-9: ordinal not in range(128). The code is:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import cgitb
cgitb.enable()

print "Content-Type: text/html;charset=utf-8"
print 
s=u'Nikolja \u043d\u0435 \u0421\u0430\u0440\u043a\u043e\u0437\u0438!'
print s#.encode('utf-8')

Yes, solution is to uncomment .encode('utf-8'), but i spend more time to understand why than happens and i cant see the answer.

2 Answers
Related