I'm studying Chinese and I wanted to create a simple flashcard CGI script. Since I'm trying to pick up python as well as Chinese, I figured I'd give it a whirl. After a little reading, I came up with the following:
#!/usr/bin/python
# largely borrowed from http://www.amk.ca/python/howto/unicode
import os, random, urllib
import cgitb; cgitb.enable()
print "Content-Type: text/html\n\n"
testing = False
if testing:
post = ''.join(
"%E6%97%A5%E6%9C%AC%E6%94%BF%E5%BA%9C%E6%AD%A3%E5%BC%8F%E8%81%B2",
"%E6%98%8E%E6%89%BF%E8%AA%8D%E6%97%A5%E8%BB%8D%E5%B1%A0%E6%AE%BA",
"%E5%8D%97%E4%BA%AC%E5%B9%B3%E6%B0%91"
)
else:
post = os.environ.get("QUERY_STRING")
post = urllib.unquote(post)
post = unicode(post, 'utf-8')
post = random.choice( list(post) )
post = post.encode('ascii', 'xmlcharrefreplace')
print post # font=+10 and center vertically and horizontally