代码: 全选
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import cgitb; cgitb.enable()
import cgi
import popen2
import string
HEAD='''
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head id="Head">
<title>Ubuntu中文在线翻译</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="在线翻译 翻译 translate">
</head>
<style type="text/css">
td.row1 { background-color: #fff8f2; }
td.row2 { background-color: #f8f5ee; }
</style>
<body>
'''
FOOD='''
</body>
'''
FORM='''
<center>
<h2>Ubuntu中文在线翻译</h2>
<form name="f">
<table cellpadding="0" cellspacing="0"><tbody><tr><td align="center"><input maxlength="2048" size="55" name="text" value="%(test)s" title="翻译单词"><br><input value="翻译" type="submit"> <input value="重写" type="reset"></td></tr></tbody></table></form>
</center>
'''
SEARCH='''
<table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td bgcolor="#9f4c09"><img alt="" height="1" width="1"></td></tr></tbody></table><table bgcolor="#e5ecf9" border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td bgcolor="#ffcf8c" nowrap="nowrap" width="1%"><font size="+1"> <b>翻译</b></font> </td><td align="right" bgcolor="#ffcf8c" nowrap="nowrap"><font size="-1">%(search)s </font></td></tr></tbody></table><BR>
'''
def trans(TEXT):
r, w, e=popen2.popen3('sdcv -n --utf8-input --utf8-output "'+TEXT+'"')
#Found 9 items, similar to xxxxx.
s=r.readline()
print string.replace(SEARCH,'%(search)s',s)
i=0;
ans={}
while 1:
s=r.readline()
if not s:break
if s[:3]=="-->":
i+=1
ans[i]=[0,1]
ans[i][0]=s[3:]
ans[i][1]=''
s=r.readline()
if s[:3]=="-->":
ans[i][0]=ans[i][0]+' [<b>'+s[3:].strip()+'</b>]'
s=r.readline()
else:
if ans[i][1]=='':
ans[i][1]=s
else:
ans[i][1]=ans[i][1]+'<BR>'+s
print '<table border="0" cellpadding="4" cellspacing="1" width="100%">'
for item in ans.keys():
print "<tr><td class='row2'>"+ans[item][0]+"</td></tr>"
print "<tr><td>"+ans[item][1]+"</td></tr>"
print "<tr><td> </td></tr>"
print "</table>"
r.close()
e.close()
w.close()
def replace(TEXT):
DICT=string.replace(TEXT.strip(),'`','')
DICT=string.replace(DICT.strip(),'~','')
DICT=string.replace(DICT.strip(),'!','')
DICT=string.replace(DICT.strip(),'@','')
DICT=string.replace(DICT.strip(),'#','')
DICT=string.replace(DICT.strip(),'$','')
DICT=string.replace(DICT.strip(),'%','')
DICT=string.replace(DICT.strip(),'^','')
DICT=string.replace(DICT.strip(),'&','')
DICT=string.replace(DICT.strip(),'*','')
DICT=string.replace(DICT.strip(),'(','')
DICT=string.replace(DICT.strip(),')','')
DICT=string.replace(DICT.strip(),'--','')
DICT=string.replace(DICT.strip(),'+','')
DICT=string.replace(DICT.strip(),'=','')
DICT=string.replace(DICT.strip(),'|','')
DICT=string.replace(DICT.strip(),'\\','')
DICT=string.replace(DICT.strip(),'{','')
DICT=string.replace(DICT.strip(),'}','')
DICT=string.replace(DICT.strip(),'[','')
DICT=string.replace(DICT.strip(),']','')
DICT=string.replace(DICT.strip(),':','')
DICT=string.replace(DICT.strip(),';','')
DICT=string.replace(DICT.strip(),'"','')
DICT=string.replace(DICT.strip(),'\'','')
DICT=string.replace(DICT.strip(),'<','')
DICT=string.replace(DICT.strip(),'>','')
DICT=string.replace(DICT.strip(),',','')
DICT=string.replace(DICT.strip(),'.','')
DICT=string.replace(DICT.strip(),'/','')
DICT=string.replace(DICT.strip(),'?','')
return DICT
def main():
print "Content-type: text/html"
print
form = cgi.FieldStorage()
if not form:
print HEAD
print string.replace(FORM,'%(test)s',"")
print FOOD
else:
print HEAD
value = form['text'].value
TEXT=cgi.escape(value)
DICT=replace(TEXT)
print string.replace(FORM,'%(test)s',DICT)
trans(DICT)
print FOOD
if __name__ == "__main__":
main()