1

我问了一个与此类似的问题,但我相信我太混乱了,无法说出我想要什么。

我有一个 cgi 脚本,我希望用户在其中输入关键字。有一个包含关键字的文件 keywords.txt。该程序将打开该文件并将其放入列表中。然后,它应该检查用户刚刚输入的关键字是否在该列表中。如果是,它将把它带到另一个 cgi 脚本,如果不是,它会将它写入文件并把它带到另一个 cgi 脚本。

我无法确定在我的 IF 语句中使用什么变量来检查用户输入的关键字是否在列表中。这是我到目前为止所拥有的,就像我说我的主要问题一样,如果弄清楚如何检查它是否在列表中,然后将它添加到文本文件中。

#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()

form = cgi.FieldStorage()
keyword = form.getvalue('keyword')



print 'Content-type: text/html\r\n\r'
print '<html>'
print '<h1>Please enter a keyword of your choice</h1>'
print '<form action="results.cgi" method="post">'
print 'Keyword: <input type="text" name="keyword">  <br />'
print '<input type="submit" value="Submit" />'
print '</form>'
print '</html>'

keylist = []
f = open('keywords.txt', 'rw')
for each in f.readlines():
    keylist.append(each)

if keyword in keylist:
    print 'Location: %s' % #URL to second cgi script

else:
    f.write(keyword)
    f.close()
    print 'Location: %s' % #URL to 3rd cgi script
4

0 回答 0