好吧,这是我的情况。我编写了一个带有文本区域的 HTML 表单,该文本区域向我的 python 脚本提交 POST 请求。我使用 cgi 库来解析文本区域并将其拆分为一个数组。然后,我使用循环处理这些项目并在处理时打印它们。看来,即使我将打印语句放入循环中,它也不会打印,直到整个过程完成并且脚本退出。 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>
see
http://docs.python.org/library/cgi.html#common-problems-and-solutions http://docs.python.org/library/cgi.html#common-problems-and-solutions
它说大多数 HTTP 服务器都会缓冲 cgi 脚本的输出直到完成。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)