mod_python代码出错!

时间:2011-05-14 18:22:16

标签: python mod-python

import cgi

def fill():
   s = """\
<html><body>
<form method="get" action="./show">
<p>Type a word: <input type="text" name="word">
<input type="submit" value="Submit"</p>
</form></body></html>
"""
   return s

# Receive the Request object
def show(req):
   # The getfirst() method returns the value of the first field with the
   # name passed as the method argument
   word = req.form.getfirst('word', '')
   print "Creating a text file with the write() method."
   text_file = open("/var/www/cgi-bin/input.txt", "w")
   text_file.write(word)
   text_file.close()
   # Escape the user input to avoid script injection attacks
   #word = cgi.escape(word)

   test(0)

   '''Input triggers the application to start its process'''

   simplified_file=open("/var/www/cgi-bin/output.txt", "r").read()
   s = """\
<html><body>
<p>The submitted word was "%s"</p>
<p><a href="./fill">Submit another word!</a></p>
</body></html>
"""
   return s % simplified_file


def test(flag):
    print flag
    while flag!=1:
        x=1
    return

这个mod_python程序的fill方法将文本发送到show方法,它写入我的应用程序使用的input.txt文件,直到我的应用程序运行,我不希望其余的语句工作,所以我调用了一个函数测试,其中我有一个while循环,它将循环连续,直到标志设置为1.如果它设置为1,那么它将打破while循环并继续执行其余的语句。 我已经让我的应用程序传递测试标志变量将其设置为1.根据我的逻辑,它应该打破循环并返回显示功能并继续执行休息,但它不会以这种方式发生,它不断加载页面!

请帮我解决这个问题。

谢谢.. :))

2 个答案:

答案 0 :(得分:2)

    while flag!=1:
        x=1

这个循环永远不会完成。 flag何时会发生变化,以致flag != 1为假?请记住,flag是一个本地变量,因此在其他地方更改它不会产生影响 - 特别是因为没有其他代码将有机会运行,而该循环是还在跑步。

你在这里想要达到的目标真的不太清楚。您不应该尝试使用无限循环来延迟代码。我会仔细考虑你的建筑。

答案 1 :(得分:0)

这不是最优雅的方法,但如果您需要更改方法之外的flag值,则应将其用作global变量。

def test():
    global flag        # use this everywhere you're using flag.
    print flag
    while flag!=1:
        x=1
    return

但是为了制作一个等待方法,看看python Event() objects,他们有一个wait()方法阻塞,直到设置了Event的标志。