如何用Python cgi打开/导入新的html文件?

时间:2013-11-01 07:46:34

标签: python cgi

如果用户输入正确,我想显示/显示test.html(与脚本位于同一目录中)。

的test.html:

<html>
<head><title>Test</title></head>
<body>
  <p>Test</p>
</body>
</html>

Form.html:

<form action="submit.cgi" method="get">
<p>Question 1</p>   
<select name="form1">
<option value="1">Test 1</option>
<option value="2">Test 2</option>
</select>

<p>Question 2</p>
<select name="form2">
<option value="3">Test 3</option>
<option value="4">Test 4</option>
</select>
</form>

Python脚本(submit.cgi):

#!/usr/bin/python3

import cgi
import cgitb

print ('Content-type: text/html\n\n')
form = cgi.FieldStorage() 
if form["form1"].value == '1' and form["form2"].value == '3':
    ...?

我知道可以在脚本中打印整个test.html(有效),但我更喜欢导入/打开html页面(尤其是多个或大型html文件)。我试过了:

open("test.html") 

但那没用。有人知道怎么做吗?

1 个答案:

答案 0 :(得分:1)

解决方案:

if form["form1"].value == '1' and form["form2"].value == '3':
    print ('''
  <head><meta http-equiv="refresh" content="0;URL='test.html'" /></head>    
''')
相关问题