2

I cannot get python CGI to print Hebrew characters to an html webpage on Linux. This is a script which demonstrates the problem:

#!/usr/bin/python3
print('Content-Type: text/html; charset=utf-8\n\n')
print ('<html><body>')
print ('first')
print ('second')
print ('תמות')
print ('third')
print ('</body></html>')

The file is saved in utf-8 (without BOM). I call this .cgi script directly from the browser address bar. The output is:

first second

While the Hebrew word and whatever follows are missing. No error shows in apache log or with cgitb enabled

I tested with apache 2.2 and python 3.2, on both Linux ubuntu 12.04 and centos 6, with Firefox, chrome and IE. And of course I can see Hebrew on any plain html page. On windows it works just fine.

Edit: while the final solution is indeed given by the linked question, this is still not a duplicate. See my comments below.

14
  • 1
    When you do a "view source" what do you see? Commented Nov 16, 2012 at 23:06
  • 2
    Try looking at stackoverflow.com/questions/3597480/… Commented Nov 16, 2012 at 23:16
  • 1
    this is not a duplicate. and the referenced question does not solve my problem, as the buffered output does not work in this context. Commented Nov 17, 2012 at 12:37
  • 1
    I agree. This is not a duplicate and the solution eyaler gave is better than in the linked question. Commented Nov 19, 2012 at 22:24
  • 2
    I want to clarify that I do not believe this question should be deleted. This is an awesome sign post for this very specific issue to a thread which provides reasonable answers. Which is why questions are closed as duplicates. Which is why I made the change since the original duplicate closure was unhelpful. Commented Nov 13, 2021 at 22:04

1 Answer 1

2

Looks like the default encoding for sys.stdout isn't necessarily UTF-8. If you want to use sys.stdout.buffer.write, try this:

sys.stdout.buffer.write('תמות'.encode('utf-8'))
Sign up to request clarification or add additional context in comments.

4 Comments

with all prints except the first (otherwise i get an error) exchanged to sys.stdout.write.buffer, i get no output at all
Can you add your new code to the question so we can see it?
print (sys.stdout.encoding) gives ANSI_X3.4-1968
with sys.stdout.buffer.write i get: Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: '_ReplOutput' object has no attribute 'buffer'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.