[完成]在线翻译的cgi脚本

软件和网站开发以及相关技术探讨
回复
头像
oneleaf
论坛管理员
帖子: 10454
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

[完成]在线翻译的cgi脚本

#1

帖子 oneleaf » 2006-07-31 19:58

谁帮我看看有没有安全漏洞之类的,如果没有,我就计划开通 dict.ubuntu.org.cn

代码: 全选

#!/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">&nbsp;&nbsp;&nbsp;&nbsp;<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">&nbsp;<b>翻译</b></font>&nbsp;</td><td align="right" bgcolor="#ffcf8c" nowrap="nowrap"><font size="-1">%(search)s&nbsp;</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]+'&nbsp;&nbsp;[<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>&nbsp;</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()
头像
oneleaf
论坛管理员
帖子: 10454
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

#2

帖子 oneleaf » 2006-08-03 10:34

出现困扰。sdcv 不支持最新的金山词霸的词典,这样就没有多大的意义了。
暂缓,知道支持为止。
回复