Section 20.6. Using Unicode with CGI


20.6. Using Unicode with CGI

In 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,         # Chinese and Japanese.         UNICODE_HELLO = u"""         Hello!         \u00A1Hola!         \u4F60\u597D!         \u3053\u3093\u306B\u3061\u306F!         """


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'         print '\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)

This script outputs Unicode strings to your Web browser.

1    #!/usr/bin/env python 2 3    CODEC = 'UTF-8' 4    UNICODE_HELLO = u''' 5    Hello! 6    \u00A1Hola! 7    \u4F60\u597D! 8    \u3053\u3093\u306B\u3061\u306F! 9    ''' 10 11   print 'Content-Type: text/html; charset=%s\r' % CODEC 12   print '\r'13print '<HTML><HEAD><TITLE>Unicode CGI Demo</TITLE></HEAD>' 14   print '<BODY>' 15   print UNICODE_HELLO.encode(CODEC) 16   print '</BODY></HTML>'

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)




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net