此代码是否正确清理用户输入?小写+“'”

时间:2013-09-29 18:23:07

标签: python cgi

我正在制作一个cgi拼写检查器,我想确保我正确清理用户输入。下面的代码删除除小写a-z和“'”之外的所有字符。我知道除了小写的a-z之外的所有东西都可以,但是小写的a-z和“'”呢。 感谢

import string
import subprocess
from time import sleep

proc = subprocess.Popen(
    ['aspell','-a'],stdout=subprocess.PIPE,
    stdin=subprocess.PIPE)
proc.stdout.readline()


text = "hello 'world' foo's bar buzz"

delete_table  = string.maketrans(
    string.ascii_lowercase+"'", ' ' * len(string.ascii_lowercase+"'")
)


def cleanq(s):
    if s == s.translate(None, delete_table): return True
    return False

def cword(w):
    try:
        if cleanq(w):
             if out[:1] == "'": return [w]
             proc.stdin.write(w+"\n")
             sleep(0.01)
             out = proc.stdout.readline()
             proc.stdout.readline()
             if out == "*\n": return [w]
             return out.split(":")[1][1:][:-1].split(", ")[:5]
        else: 
            return [w]
    except:
        return [w]


for w in text.split(" "):
    print cword(w)


proc.kill()

0 个答案:

没有答案