Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實(shí)例
table通過(guò)使用下面語(yǔ)句創(chuàng)建:
更快地插入數(shù)據(jù)
在此用time.clock()來(lái)計(jì)時(shí),看看以下三種方法的速度。
import sqlite3
import time
def create_tables(dbname):
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
cursor.execute('''create table userinfo(name text, email text)''')
conn.commit()
cursor.close()
conn.close()
def drop_tables(dbname):
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
cursor.execute('''drop table userinfo''')
conn.commit()
cursor.close()
conn.close()
def insert1():
users = [('qq','qq@example.com'),
('ww','ww@example.com'),
('ee','ee@example.com'),
('rr','rr@example.com'),
('tt','tt@example.com'),
('yy','yy@example.com'),
('uu','uu@example.com')
]
start = time.clock()
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
for user in users:
cursor.execute("insert into userinfo(name, email) values(?, ?)", user)
conn.commit()
cursor.close()
conn.close()
end = time.clock()
print start, end, end-start
def insert2():
users = [('qq','qq@example.com'),
('ww','ww@example.com'),
('ee','ee@example.com'),
('rr','rr@example.com'),
('tt','tt@example.com'),
('yy','yy@example.com'),
('uu','uu@example.com')
]
start = time.clock()
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
for user in users:
cursor.execute("insert into userinfo(name, email) values(?, ?)", user)
conn.commit()
cursor.close()
conn.close()
end = time.clock()
print start, end, end-start
def insert3():
users = [('qq','qq@example.com'),
('ww','ww@example.com'),
('ee','ee@example.com'),
('rr','rr@example.com'),
('tt','tt@example.com'),
('yy','yy@example.com'),
('uu','uu@example.com')
]
start = time.clock()
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
cursor.executemany("insert into userinfo(name, email) values(?, ?)", users)
conn.commit()
cursor.close()
conn.close()
end = time.clock()
print start, end, end-start
if __name__ == '__main__':
dbname = 'test.db'
create_tables(dbname)
insert1()
drop_tables(dbname)
create_tables(dbname)
insert2()
drop_tables(dbname)
create_tables(dbname)
insert3()
drop_tables(dbname)
某次運(yùn)行結(jié)果:
4.05223164501e-07 0.531585119557 0.531584714334
0.755963264089 0.867329935942 0.111366671854
1.0324360882 1.12175173111 0.0893156429109
另外一次運(yùn)行結(jié)果:
4.05223164501e-07 0.565988971446 0.565988566223
0.768132520942 0.843723660494 0.0755911395524
1.04367819446 1.13247636739 0.0887981729298
在運(yùn)行結(jié)果中,第三列表示插入數(shù)據(jù)使用的時(shí)間。綜合看來(lái),方法insert1()的速度很慢,原因在于每次insert都commit()。
更安全地操作數(shù)據(jù)庫(kù)
先上代碼:
import sqlite3
def create_tables(dbname):
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
cursor.execute('''create table userinfo(name text, email text)''')
conn.commit()
cursor.close()
conn.close()
def drop_tables(dbname):
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
cursor.execute('''drop table userinfo''')
conn.commit()
cursor.close()
conn.close()
def insert():
users = [('qq','qq@example.com'),
('ww','ww@example.com'),
('ee','ee@example.com'),
('rr','rr@example.com'),
('tt','tt@example.com'),
('yy','yy@example.com'),
('uu','uu@example.com')
]
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
cursor.executemany("insert into userinfo(name, email) values(?, ?)", users)
conn.commit()
cursor.close()
conn.close()
def insecure_select(text):
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
print "select name from userinfo where email='%s'" % text
for row in cursor.execute("select name from userinfo where email='%s'" % text):
print row
def secure_select(text):
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
print "select name from userinfo where email='%s'" % text
for row in cursor.execute("select name from userinfo where email= ? ", (text,)):
print row
if __name__ == '__main__':
dbname = 'test.db'
create_tables(dbname)
insert()
insecure_select("uu@example.com")
insecure_select("' or 1=1;--")
secure_select("uu@example.com")
secure_select("' or 1=1;--")
drop_tables(dbname)
運(yùn)行結(jié)果:
select name from userinfo where email='uu@example.com'
(u'uu',)
select name from userinfo where email='' or 1=1;--'
(u'qq',)
(u'ww',)
(u'ee',)
(u'rr',)
(u'tt',)
(u'yy',)
(u'uu',)
select name from userinfo where email='uu@example.com'
(u'uu',)
select name from userinfo where email='' or 1=1;--'
函數(shù)insecure_select(text)和secure_select(text)的本意都是根據(jù)email獲取對(duì)應(yīng)的用戶名信息。但是insecure_select(text)的實(shí)現(xiàn)容易引起sql注入。
insecure_select("' or 1=1;--")便是一個(gè)例子。在insecure_select()中cursor.execute()只有一個(gè)參數(shù),即sql語(yǔ)句,這個(gè)生成的sql語(yǔ)句如果有問(wèn)題,還是會(huì)照常執(zhí)行。
secure_select(text)的實(shí)現(xiàn)可以防止sql注入,cursor.execute()的第一個(gè)參數(shù)使用了占位符?表示要被替代的內(nèi)容,第二個(gè)參數(shù)指定每個(gè)占位符對(duì)應(yīng)的值,在底層實(shí)現(xiàn)上,這種方法(至少)轉(zhuǎn)義了特殊字符,可以防止sql注入。
- python環(huán)境功能強(qiáng)大的pip-audit安全漏洞掃描工具
- Python線程之線程安全的隊(duì)列Queue
- 詳細(xì)總結(jié)Python常見(jiàn)的安全問(wèn)題
- 使用Python實(shí)現(xiàn)一個(gè)安全封裝EXE文件加密保護(hù)工具
- Python?Bleach保障網(wǎng)絡(luò)安全防止網(wǎng)站受到XSS(跨站腳本)攻擊
- python 安全地刪除列表元素的方法
- Python從零打造高安全密碼管理器
- Python hashlib庫(kù)數(shù)據(jù)安全加密必備指南
- Python實(shí)現(xiàn)安全密碼生成器的示例代碼
- Python如何保護(hù)代碼和數(shù)據(jù),常見(jiàn)的安全漏洞及應(yīng)對(duì)措施有哪些?
相關(guān)文章
Python提取PDF發(fā)票信息保存Excel文件并制作EXE程序的全過(guò)程
之前零散的用過(guò)一點(diǎn)python做數(shù)據(jù)處理,這次又遇到一個(gè)數(shù)據(jù)處理的小功能,下面這篇文章主要給大家介紹了關(guān)于Python提取PDF發(fā)票信息保存Excel文件并制作EXE程序的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
基于Python實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物檢測(cè)功能
基于dlib庫(kù)的模型,實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物的檢測(cè)。最后呈現(xiàn)的效果為焦點(diǎn)人物的識(shí)別框顏色與其他人物框不一樣。對(duì)Python人臉識(shí)別和焦點(diǎn)人物檢測(cè)設(shè)計(jì)過(guò)程感興趣的朋友一起看看吧2021-10-10
pytorch + visdom 處理簡(jiǎn)單分類問(wèn)題的示例
這篇文章主要介紹了pytorch + visdom 處理簡(jiǎn)單分類問(wèn)題的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Python之try無(wú)法使用全局變量的問(wèn)題解決
當(dāng)我們使用try語(yǔ)句時(shí),如果在try中使用了全局變量,但又在except或finally中修改了這個(gè)全局變量,就會(huì)出現(xiàn)這種無(wú)法修改全局變量的情況,下面就來(lái)解決一下這個(gè)問(wèn)題,感興趣的可以了解一下2024-08-08
完美解決Django2.0中models下的ForeignKey()問(wèn)題
這篇文章主要介紹了完美解決Django2.0中models下的ForeignKey()問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python 函數(shù)詳解:從基礎(chǔ)語(yǔ)法到高級(jí)使用技巧
本文基于實(shí)例代碼,全面講解 Python 函數(shù)的定義、參數(shù)傳遞、變量作用域及類型標(biāo)注等知識(shí)點(diǎn),幫助初學(xué)者快速掌握函數(shù)的使用技巧,感興趣的朋友跟隨小編一起學(xué)習(xí)吧2025-08-08
Python requests請(qǐng)求超時(shí)的解決方案
在進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)爬取過(guò)程中,網(wǎng)絡(luò)請(qǐng)求超時(shí)是一個(gè)令人頭疼的問(wèn)題,尤其在Python中,我們常常需要應(yīng)對(duì)各種網(wǎng)絡(luò)爬蟲(chóng)、API調(diào)用或其他網(wǎng)絡(luò)操作,而網(wǎng)絡(luò)請(qǐng)求超時(shí)的原因千奇百怪,在本篇文章中,我們將深入探討Python requests請(qǐng)求超時(shí)的解決方案,需要的朋友可以參考下2024-12-12

