0
点赞
收藏
分享

微信扫一扫

python 非法字符处理


有勇气的牛排
官方地址: ​​​https://www.couragesteak.com/​​

过滤非法字符

def sql_filter(sql, max_length=20):
dirty_stuff = ["\"", "\\", "/", "|", "&", "*", "'", "=", "-", "#", ";", "<", ">", "+", "%", "$", "(", ")", "%", "@","!"]
for stuff in dirty_stuff:
sql = sql.replace(stuff, "")
return sql[:max_length]

username = "1234567890!@#!@#!@#$%======$%|&***"

username = sql_filter(username) # SQL注入
print(username)

# 输出结果是:1234567890

检测是否包含非法字符

import re

# username = "1234567890!@#!@#!@#$%======$%|&***"
username = "1234567890*"

# 检测到非法字符进入if
if not re.search(u'^[_a-zA-Z0-9\u4e00-\u9fa5]+$', username):
msg = u"用户名不可以包含非法字符(!,@,#,$,%...)"
print(msg)

举报

相关推荐

0 条评论