簡體   English   中英

Python:腳本退出前CGI更新網頁

[英]Python: CGI update webpage before script exit

好的,這是我的情況。 我編寫了一個帶有textarea的HTML表單,該表單向我的python腳本提交POST請求。 我使用cgi庫解析文本區域並將其拆分為一個數組。 然后,我使用循環處理項目並在處理項目時打印它們。 似乎即使我將print語句放入循環中,它也不會打印出來,直到整個過程完成並且腳本已退出。 html頁面也將掛起,直到該過程完成。

我試圖在每個打印語句之后使用sys.std.flush()刷新緩沖區,但是它沒有幫助。

看下面我的代碼。 所有幫助將不勝感激。

提前致謝。

import cgi
import urllib2

form = cgi.FieldStorage()

h = str(form.getvalue('list'))

list = h.split('\r\n');
try:
    print "Content-Type: text/html\n\n"
    print "<head>"
    print "<title>Age</title>"
    print "</head>"
    print "<body>"
    for x in range(0, len(list)):
        print "List: %s" %list[x] + "</br>"
        sck = urllib2.urlopen("http://www.google.com/")
        data = sck.read()
        if len(data) > 0:
            print "Data collected<br />"
        sck.close
    print "</body>"
except Exception:
    pass


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Python Demo</title>
<style type="text/css">
    #form {
        width: 400px;
    }

    #form label {
        display: block;
    }

    #form textarea {
        width: 100%;
    }

    #form input {
        display: block;
        margin-top: 10px;
    }
</style>
</head>

<body>
<div id="form">
<form id="form1" name="form1" method="post" action="demo.py">
    <label for="list">List:</label>
    <textarea name="list" id="list" cols="50" rows="10"></textarea>
    <input type="submit" name="submit" id="submit" value="Submit" />
</form>
</div>
</body>
</html>

看到

http://docs.python.org/library/cgi.html#common-problems-and-solutions

它說大多數HTTP服務器都會緩沖cgi腳本的輸出,直到完成為止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM