如何在python中转到其他脚本之前对用户进行身份验证

时间:2018-04-25 13:33:34

标签: python

我是python的新手,我正在尝试用python构建一个网站,它有2个网页。 '的login.html' 'details.html'

但用户无需登录即可直接进入详细信息页面,如何停止此操作?

当我使用login_required使用django时,我能够这样做,但是当我使用cgi-bin脚本python时,对于webportal我该如何阻止它?

请帮我解决这个问题。

以下是示例代码:

的login.html

<form action = "login1.py" method = "POST">
<label for="uname"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="uname" required>
<br>
<br>
    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" required>
<br>
<br>
<br>
    <button type="submit">Login</button>

details.html

<form action = "copy1.py" method = "POST">
<label for="phone num">Phone num:</label>
 <input type = "text" name = "phone num">  <br />
<br >
<br >
<label for="email">Destination path :</label>
 <input type = "text" name = "email" />
 <br>
<br >
<br >
<div id=submit>
<input type = "submit" value = "Submit" />

login.py:

print("Content-type: text/html")
print("")
import cgi,cgitb
def func():

    form = cgi.FieldStorage()
    cgitb.enable()
    username=form.getvalue('uname')
    password  = form.getvalue('psw')
userlist=[x,g,d]
passlist=[m,ldf]
if username in userlist:
            if password in passlist:
                    redirectURL = "http://localhost/test/details.html"
                    print('<html>')
                    print('  <head>')
                    print('    <meta http-equiv="refresh" content="0;url='+str(redirectURL)+'" />')
                    print('  </head>')
                    print('</html>')
            else:
                    print('<html>')
                    print('<div id="topcorner"> ')
                    print("please enter valid password")
                    print('</div>')
                    print('</html>')
    else:
            print('<html>')
            print('<div id="topcorner"> ')
            print("please enter valid username")
            print('</div>')
            print('</html>')

问题: 如果没有登录,我也可以打开details.html,用户首先应该在访问其他网站之前进行身份验证。 请帮帮我

1 个答案:

答案 0 :(得分:0)

如果问题不在于身份验证,请尝试在脚本中添加以下行,

print('Content-Type: text/html')
print('Location: {}'.format(redirectURL))
print() # HTTP says you have to have a blank line between headers and content
print('<html>')
print('  <head>')
...