mod_python 代码错误!

发布于 2024-11-07 06:16:34 字数 1467 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情勿念 2024-11-14 06:16:34
    while flag!=1:
        x=1

这个循环永远不会结束。 flag 什么时候会发生变化,使得 flag != 1 为 False?请记住,flag 是一个本地变量,因此在其他任何地方更改它不会产生效果 - 特别是因为没有其他代码有机会在该循环仍在运行。

实际上还不是很清楚你想在这里实现什么目标。您不应该尝试用无限循环来延迟代码。我会仔细重新考虑你的架构。

    while flag!=1:
        x=1

This loop won't ever finish. When is flag ever going to change so that flag != 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.

剩余の解释 2024-11-14 06:16:34

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

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

但要创建等待方法,请查看 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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文