简单的python登录操作

html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>python登录页面</title>
</head>
<body>
<center>
<h1>python登录页面</h1>
<table border="1">
<tr>
<td>账号</td>
<td><input type="text" name="name" id="name"><span id="sname"></span></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd" id="pwd"><span id="spwd"></span></td>
</tr>
<tr>
<td colspan="2">
<input type="reset">
<input type="submit" value="登录" id="submit">
</td>
</tr>
</table>
</center>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$('#name').blur(function(){
if($(this).val()==""){
$("#sname").html("*账号不能为空")
return false
}else{
$("#sname").html("")
return true
}
})
$('#pwd').blur(function(){
if($(this).val()==""){
$("#spwd").html("*密码不能为空")
return false
}else{
$("#spwd").html("")
return true
}
})
$("#submit").click(function(){
var bool1 = $('#name').triggerHandler("blur");
var bool2 = $('#pwd').triggerHandler("blur");
if(bool1&&bool2){
name = $('#name').val()
pwd = $('#pwd').val()
$.ajax({
type: "POST",
url: "./htbin/login.py",
data: {name:name,pwd:pwd},
success: function(msg){
if(msg==-1){
alert("用户不存在")
}else if(msg==0){
alert("密码错误")
}else{
alert("登录成功")
}
}
});
}else {
return false;
}
})
</script>



python页面
print('Content-Type: text/html\n')
import cgi
import pymysql
form = cgi.FieldStorage()
name = form['name'].value
pwd = form['pwd'].value
#连接数据库 选择数据库
db = pymysql.connect("127.0.0.1","root","root","other",charset='utf8')
#创建游标
cursor = db.cursor()
#sql语句
sql = "select * from string where name='"+name+"'"
#执行sql
cursor.execute(sql)
data = cursor.fetchone()
if data == None:
print(-1)
else:
if data[2]==pwd:
print(1)
else:
print(0)

猜你喜欢

转载自blog.csdn.net/weixin_36691991/article/details/88623519