Python CGI URL 重定向

0 投票
1 回答
556 浏览
提问于 2025-04-19 22:07

我现在正在写一个CGI的Python脚本。等我有时间了会把它重写成web2py,但现在实在没时间。

我已经把大部分逻辑都搭建好了,就差一件事。我需要做到:

1) 发送一个变量来启动一个进程(这个我已经搞定了)
2) 刷新页面直到这个进程结束
3) 一旦进程完成,就显示相关信息。

我好像卡在了URL刷新这一步,无法保留启动原始进程的变量。

我试过用webbrowser(webbrowser.open('http://example.com?running=1')),但不知道为什么在Mac上我根本没有被重定向。

if print_html.parse_url():
    url_variable=print_html.parse_url()
    IP=url_variable['IP'].value
    Iterations=int(url_variable['quantity'].value)
    start=url_variable['start'].value
    refresh=url_variable['refresh'].value
if start == "1":

如你所见,我从URL中读取变量并赋值。当start == '1'时,我想开始运行后面的程序。在程序运行时,我想改变URL中的变量,以便重新读取页面,直到所有处理完成。

再多解释一下,也许这能帮助理解: 我需要刷新页面,或者打开同一个页面但带有不同的变量。

例如: 第一次实例: http://example.com/test.py?start=1 逻辑运行后,然后刷新生成: 第二次实例: http://example.com/test.py?running=1

这样说清楚了吗?

1 个回答

0

你可以通过HTML中的 meta http-equiv 指令来刷新页面。

#!/usr/bin/python
import datetime
import time

print "Content-Type: text/html"
print ""
print '''
<html>
<head>
<meta http-equiv="refresh" content="15" />
</head>'''

now = datetime.datetime.now()
now = now.isoformat()

print '''
<body>
The time is now %s
</body>''' % now

撰写回答