Python 重定向获取错误时,标题格式错误

Python 重定向获取错误时,标题格式错误,python,mysql,apache,Python,Mysql,Apache,前几天我在stack overflow中看到了如何使用函数重定向。。 但它在这里不起作用 #!/Python27/python import cgi import cgitb ; cgitb.enable() import MySQLdb from datetime import datetime f= cgi.FieldStorage() l=f["roll"].value def redirect(url): print "Content-Type: text/plain"

前几天我在stack overflow中看到了如何使用函数重定向。。 但它在这里不起作用

#!/Python27/python
import cgi
import cgitb ; cgitb.enable()
import MySQLdb
from datetime import datetime
f= cgi.FieldStorage()
l=f["roll"].value
def redirect(url):
    print "Content-Type: text/plain"
    print "Refresh: 0; url='%s'" % url
    print
    print "Redirecting..."

Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="root", passwd="jesus",        db="student")
cursor = Con.cursor()
sql="SELECT YEAR FROM STDDET WHERE id='%s'"%(l)
cursor.execute(sql)
data=cursor.fetchone()
cyear=datetime.now().year
cmonth=datetime.now().month
data=data[0]
data=int(data)
year=cyear-data
print year
if(year==3):
    redirect("intro.html")
elif(year==4):
     redirect("yea4.html")

即使if条件满足,它也不会被重定向…我收到一个格式错误的标题…plz帮助

据我所知,您的脚本会产生以下响应:

Content-Type: text/plain Refresh: 0; url='x.y.com'

Redirecting...
这不是有效的http响应,因为缺少

如果生成一个“有效”页面,则应回复状态

HTTP/1.1 302 Found
如果您正在重定向,最好使用3xx代码之一进行回复。例如:

HTTP/1.1 302 Found
Location: http://www.google.fr/

HTTP/1.1…
行必须是标题的第一行。因此,它应该由程序的第一次
打印产生。请参见示例。

请修复代码缩进。我是否可以使用http头响应根据条件重定向…我编辑了代码…您是否可以告诉我http头是否适用于该代码??如果没有,请帮我回答