python - Apache在重定向时显示代码而不是网页
问题描述
我有一个带有登录表单的网页,该表单由 python 脚本处理。当我输入在数据库中找不到的名称时,它会重定向到 login_or_signup.html 页面。当我输入数据库中的名称时,应该重定向到“next_page.html”页面,但它显示了一些代码。为什么它以一种方式工作而不是另一种?(我已将其更改为双向访问 login_or_signup.html 页面,并且双向工作。
Python代码
#! /usr/bin/python
import cgi
import cgitb
import mysql.connector as conn
import passlib
from passlib.hash import argon2
import logging
cgitb.enable(logdir="/var/www/html",
display=False,
format='text',
)
logging.basicConfig(level=logging.INFO)
def basic_top(data):
print """Content-type:text/html\n\n
<DOCTYPE html>
<html lang=en">
<head>
<meta charset= utf-8/>
<title>Basic Version</title>
</head>
<body>"""
for d in data:
print d
print
def go_back_top(data):
redirectURL = "%s" % "/login_or_signup.html"
try:
print """Content-type:text/html\n\n
Location: %s""" % redirectURL
print """<DOCTYPE html>
<html lang="en">
<head>
<meta charset= utf-8/>"""
print " <meta http-equiv='refresh' content='0;url=%s' />" % redirectURL
print """ <title>Login</title>
</head>\n\n
<body>"""
#for d in data:
# print d
# print
print ' Redirecting... <a href="%s">Click here if you are not redirected</a>' % redirectURL
except:
cgi.print_exception()
print "An error occured\n\n"
print"<PRE>"
cgitb.print_exc()
def go_forwards_top(data):
redirectURL = "%s" % "/login_or_signup.html" #/next_page.html"
try:
print """Content-type:text/html\n\n
Location: %s""" % redirectURL
print """<DOCTYPE html>
<html lang="en">
<head>
<meta charset= utf-8/>"""
print " <meta http-equiv='refresh' content='0;url=%s' />" % redirectURL
print """ <title>Login</title>
</head>\n\n
<body>"""
print ' Redirecting... <a href="%s">Click here if you are not redirected</a>' % redirectURL
except:
cgi.print_exception()
print "An error occured\n\n"
print"<PRE>"
cgitb.print_exc()
def htmlTail():
print "</body>"
print "</html>"
def getData():
formdata = cgi.FieldStorage()
username = formdata.getvalue("username")
password = formdata.getvalue("password")
login_type = formdata.getvalue("type")
if login_type == "signup":
password_copy = formdata.getvalue("password_copy")
return username, password, password_copy, login_type
else:
return username, password, login_type
def connectDB():
db = conn.connect(user='basic_user', password='twathead', host='127.0.0.1', db="exampleDB")
cursor = db.cursor()
logging.info(cursor)
return db, cursor
def check_person(cursor, data):
username = data[0]
sql_str = "SELECT * FROM people WHERE username = '%s'" % (username)
cursor.execute(sql_str)
person = cursor.fetchone()
if person is None:
go_back_top(data)
else:
go_forwards_top(data)
#logging.info(str(conn))
#conn.close()
if __name__=="__main__":
logging.basicConfig(filename='/usr/lib/cgi-bin/example.log', level=logging.INFO)
logging.info('This message should go to the log file')
#data = ("John", "Smith")
data = getData()
#basic_top(data)
#print "login type = "
#print data[0]
db, cursor = connectDB()
check_person(cursor, data)
next_page 的 HTML:
<DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="mystyle.css">
<title>Post Login</title>
</head>
<body style="background-color:powderblue;">
<h2>This page is currently under construction.<h2>
</body>
</html>
解决方案
打开终端,输入:
服务 apache-htcacheclean 重启
推荐阅读
- can-bus - CAN总线从两个平衡的主机发送数据
- node.js - 限制并发请求 + node.js
- php - PHP 警告:shell_exec(): 无法执行 '[command]'
- javascript - 为什么 Chrome Mobile 取消 setPointerCapture?我如何解决它?
- linux - 转换后将文件移动到其原始位置
- mongodb - MongoDB mongoose findOneAndUpdate() Await 不等待。A3 在 A2 之前运行
- laravel - Laravel API:“错误:证书已过期”虽然证书处于活动状态
- windows - 如何将命令行参数传递给由 doskey 调用的 PowerShell 脚本
- php - 在 PHP 7 中根据包含日期的值删除数组元素
- c# - 在 ASP.Net Web API 控制器中具有相同路由名称的 Post Action 方法重载