>: ").strip() pwd=input(" /> >: ").strip() pwd=input(" />
python代码连接Mysql数据库 有bug(sql注入的问题): #pip3 install pymysql import pymysql user=input("user>>: ").strip() pwd=input(
有bug(sql注入的问题):
#pip3 install pymysql
import pymysql
user=input("user>>: ").strip()
pwd=input("password>>: ").strip()
# 建立链接
conn=pymysql.connect(
host="192.168.10.15",
port=3306,
user="root",
passWord="123",
db="db9",
charset="utf8"
)
# 拿到游标
cursor=conn.cursor()
# 执行sql语句
sql="select * from userinfo where user = "%s" and pwd="%s"" %(user,pwd)
rows=cursor.execute(sql)
cursor.close()
conn.close()
# 进行判断
if rows:
print("登录成功")
else:
print("登录失败")
在sql语句中 --空格 就表示后面的被注释了,所以密码pwd就不验证了,只要账户名对了就行了,这样跳过了密码认证就是sql注入
这样的连用户名都不用知道都可以进入,所以在注册账户名时好多特殊字符都不让使用,就怕这个.
改进版(防止sql注入)
#pip3 install pymysql
import pymysql
user=input("user>>: ").strip()
pwd=input("password>>: ").strip()
# 建立链接
conn=pymysql.connect(
host="192.168.10.15",
port=3306,
user="root",
password="123",
db="db9",
charset="utf8"
)
# 拿到游标
cursor=conn.cursor()
# 原来的执行sql语句
# sql="select * from userinfo where user = "%s" and pwd="%s"" %(user,pwd)
# print(sql)
# 现在的
sql="select * from userinfo where user = %s and pwd=%s"
rows=cursor.execute(sql,(user,pwd))
#原来是在sql中拼接,现在是让execute去做拼接user和pwd
cursor.close()
conn.close()
# 进行判断
if rows:
print("登录成功")
else:
print("登录失败")
--结束END--
本文标题: pymysql
本文链接: https://www.lsjlt.com/news/5732.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0