在我的代碼中,我嘗試從我的數據庫(delete_table函數)中刪除一個表。就在此之後,我檢查表是否已被刪除(check_table_from_db函數)。當我這樣做時,表被刪除,但check_table_from_db函數顯示它不是。當我在兩個不同的運行(兩個不同的程序)中執行此過程時。結果是正確的。 以下是check_table_from_db代碼:Python MySQLdb:表不會立即刪除
def check_table_from_db(table_name):
import MySQLdb as mdb
try:
exists = False
con = mdb.connect('localhost', 'user', '123', 'test')
cur = con.cursor()
s = "SHOW TABLES LIKE '%s'" % (table_name)
cur.execute(s)
exists = (cur.rowcount > 0)
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
finally:
if con:
cur.close()
con.close()
return exists
編輯: 既然已被要求發佈的delete_table功能,我需要解釋什麼是通用的。 delete_table是一個模仿SQL注入攻擊的函數。如果攻擊成功,那麼表將從數據庫中刪除(我通過check_table_from_db檢查了這一點)。它在網站的一個字段中注入代碼。至於刪除表這樣的代碼是:
attack_script = "');DROP TABLE test2;"
_resp = utilities.submit_form(utilities.get_absolute_url(fd.get('action'), main_url), {'uname':'test', 'password':'test', 'cpassword':'test', 'fname':'', 'lname':'', 'age':'', 'major':'', 'university':attack_script}, method=fd.get('method'))
通過submit_form功能,我在填充網站的形式和我提交。一旦提交,我然後檢查表是否被刪除。
請也給了'delete_table'的代碼。 – 2015-04-04 17:28:03