[英]Error in mod_python code!
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.根据我的逻辑,它应该打破循环并返回显示功能并继续执行休息,但它不会以这种方式发生,它不断加载页面!
请帮我解决这个问题..
谢谢.. :)
while flag!=1:
x=1
这个循环永远不会完成。 flag
何时会改变,以至于flag != 1
是假的? 请记住, flag
是一个局部变量,因此在其他地方更改它不会产生影响 - 特别是因为在该循环仍在运行时没有其他代码可以运行。
你在这里想要实现的目标真的不太清楚。 您不应该尝试使用无限循环来延迟代码。 我会仔细考虑你的架构。
它不是最优雅的方法,但是如果你需要在方法之外更改flag的值,你应该将它用作global
变量。
def test():
global flag # use this everywhere you're using flag.
print flag
while flag!=1:
x=1
return
但是为了制作一个等待方法,看一下python Event()对象 ,它们有一个wait()方法阻塞,直到设置了Event的标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.