Apache在重定向时显示的代码不是网页

时间:2019-02-09 13:33:01

标签: python html apache cgi url-redirection

我有一个带有登录表单的网页,该网页由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)  

下一页的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>

1 个答案:

答案 0 :(得分:0)

打开终端,输入:

服务apache-htcacheclean重新启动