20.6. Using Unicode with CGIIn Chapter 6, "Sequences," we introduced the use of Unicode strings. In Section 6.8.5, we gave a simple example of a script that takes a Unicode string, writing it out to a file and reading it back in. In this section, we will demonstrate a simple CGI script that has Unicode output and how to give your browser enough clues to be able to render the characters properly. The one requirement is that you must have East Asian fonts installed on your computer so that the browser can display them. To see Unicode in action we will build a CGI script to generate a multilingual Web page. First of all we define the message in a Unicode string. We assume your text editor can only enter ASCII. Therefore the non-ASCII characters are input using the \u escape. In practice the message can also be read from a file or from database. # Greeting in English, Spanish, The first output the CGI generates is the content-type HTTP header. It is very important to declare here that the content is transmitted in the UTF-8 encoding so that the browser can correctly interpret it. print 'Content-type: text/html; charset=UTF-8\r' Then output the actual message. Use the string's encode() method to translate the string into UTF-8 sequences first. print UNICODE_HELLO.encode('UTF-8') Example 20.7 shows the complete program. Example 20.7. Simple Unicode CGI Example (uniCGI.py)
If you run the CGI code from your browser, you will get output like that shown in Figure 20-13. Figure 20-13. Simple Unicode CGI demo output in Firefox (uniCGI.py)[View full size image] |
Wednesday, October 21, 2009
Section 20.6. Using Unicode with CGI
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment