", "+", "%", "$", "(", ")", "%", "@","!"] for stuff in dirty_stuff: sql = sql.replace(stuff, "") return _python非法字符怎么处">
当前位置:   article > 正文

python 非法字符处理_python非法字符怎么处理

python非法字符怎么处理

过滤非法字符

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
'
运行

检测是否包含非法字符

import re

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

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

参考:
https://www.cnblogs.com/cybermat/articles/473314.html
https://blog.csdn.net/weixin_30707875/article/details/99341309

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/893621
推荐阅读
相关标签
  

闽ICP备14008679号