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。根据我的逻辑,它应该打破循环并返回到显示函数并继续执行其余部分,但它不会以这种方式发生,它会不断加载页面!
请帮助我解决这个问题..
谢谢..:)
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
This mod_python program's fill method send the text to show method where it writes to input.txt file which is used by my application, till my application is running i don't want rest of the statements to work so i have called a function test, in which i have a while loop which will be looping continuously till the flag is set to 1. If its set to 1 then it will break the while loop and continue execution of rest of the statements.
I have made my application to pass test flag variable to set it as 1. according to my logic, it should break the loop and return to show function and continue executing rest but its not happening in that way, its continuously loading the page!
please help me through this..
Thank you.. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个循环永远不会结束。
flag
什么时候会发生变化,使得flag != 1
为 False?请记住,flag
是一个本地变量,因此在其他任何地方更改它不会产生效果 - 特别是因为没有其他代码有机会在该循环仍在运行。实际上还不是很清楚你想在这里实现什么目标。您不应该尝试用无限循环来延迟代码。我会仔细重新考虑你的架构。
This loop won't ever finish. When is
flag
ever going to change so thatflag != 1
is False? Remember,flag
is a local variable so changing it anywhere else isn't going to have an effect -- especially since no other code is going to have the opportunity to run while that loop is still running.It's really not very clear what you're trying to achieve here. You shouldn't be trying to delay code with infinite loops. I'd re-think your architecture carefully.
这不是最优雅的方法,但如果您需要在方法外部更改 flag 的值,则应该将其用作全局变量。
但要创建等待方法,请查看 python Event() 对象,它们有一个 wait() 方法,该方法会阻塞,直到事件的标志被设置。
it is not the most elegant way to do this, but if you need to change the value of flag outside your method, you should use it as a
global
variable.but to make a waiting method, have a look to python Event() objects, they have a wait() method that blocks until the Event's flag is set.