首页 >社区问答列表 >python 操作mysql如何经量防止自己的程序在之后被恶意注入(说白了就是问一下python防注入的一些要点)

python 操作mysql如何经量防止自己的程序在之后被恶意注入(说白了就是问一下python防注入的一些要点)

有没有必要参考以往的php或者java之类的语言的防注入文档呢?python相关的防注入文档实在不好找

  • 黄舟
  • 黄舟    2017-06-14 10:52:542楼

    1.通过

    cursor.execute("select * from table where name=%s", "name")
    

    可以防注入。

    2.如果通过

    sql = "select * from table where name=%s" % MySQLdb.escape_string(name)
    

    这种格式,需要MySQLdb.escape_string(name),可以防注入.

    推荐使用第一种。

    +0添加回复

  • 回复
  • 欧阳克
  • 欧阳克    2017-06-14 10:52:541楼

    sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
    cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
    

    我知道的一点是不要使用 sql.format("x1", "x2"), 要把参数传给cursor.execute去处理

    +0添加回复

  • 回复