此代码从 html 中获取数据并在文件中搜索。如果找到搜索,它将在 Web 浏览器上打印。现在我询问为什么 found = False 写在 b.append() 行之后。b'coz 如果我们不写它会在网络浏览器上打印文件中的所有数据
#!/usr/bin/python
import cgi
def get_search():
found = False
form = cgi.FieldStorage()
Fname = form.getvalue('firstname', '')
Lname = form.getvalue('lastname', '')
Age = form.getvalue('age', 0)
Gender = form.getvalue('gender', '')
f = open("/tmp/abcd.txt","r")
b = []
for line in f:
temp = line.split()
Fsearch = temp[0]
Lsearch = temp[1]
Asearch = temp[2]
Gsearch = temp[3]
if Fname and Fname.lower() in Fsearch.lower():
found = True
if Lname and Lname.lower() in Lsearch.lower():
found = True
if Age and Age == Asearch:
found = True
if Gender and Gender in Gsearch:
found = True
if found:
b.append(line)
found = False
f.close()
print "Content-type:text/html\n"
if len(b) == 0:
print "No records found"
else:
for each in b:
print each
get_search()