1 Star 0 Fork 10

superdebug/pydht

forked from 元谷/pydht 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
db.py 4.03 KB
一键复制 编辑 原始数据 按行查看 历史
元谷 提交于 2014-02-12 23:46 . 修改存库多线程
# -*- coding: cp936 -*-
import time
import psycopg2
import Queue,threading
from seedAnalyse import SeedAnalyse
def btInfoSave(hash_id):
conn= psycopg2.connect(host="localhost",user="postgres",password="123456",dbname="dht")
cursor = conn.cursor()
sql = "select * from bt where hash= '" + hash_id + "'"
#param = (hash_id)
try:
cursor.execute(sql)
except Exception,ex:
print Exception,":",ex
return
if cursor.fetchone(): return #存在数据
#分析数据
s = SeedAnalyse()
seed = s.analyse(hash_id)
if not seed: return
info = seed['info']
name = info['name']
sql = "INSERT INTO bt(hash,name) values (%s,%s)"
param = (hash_id, name )
try:
cursor.execute(sql,param)
except Exception,ex:
print Exception,":",ex
return
conn.commit()
sql = "select * from bt where hash='" + hash_id + "'"
cursor.execute(sql)
nid = cursor.fetchone()[0]
lenght = 0
if 'files' in info:
for nfile in info['files']:
lenght = nfile['length']
name = nfile['path']
sql = "INSERT INTO file(name,size,btid) values (%s,%s,%s)"
param = ( name, str(lenght), str(nid))
try:
cursor.execute(sql,param)
except Exception,ex:
print Exception,":",ex
return
else:
lenght = info['length']
sql = "INSERT INTO file(name,size,btid) values (%s,%s,%s)"
param = ( name, str(lenght), str(nid))
try:
cursor.execute(sql,param)
except Exception,ex:
print Exception,":",ex
return
conn.commit()
conn.close()
class DbThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.conn= psycopg2.connect(host="localhost",user="postgres",password="123456",dbname="dht")
self.cursor = self.conn.cursor()
self.queue = Queue.Queue()
def run(self):
while True:
if self.queue.qsize()> 0:
hash_id = self.queue.get()
btInfoSave(hash_id)
else:
time.sleep(1)
def put(self, hash_id):
self.queue.put(hash_id)
def __btInfoSave(self, hash_id):
sql = "select * from bt where hash= '" + hash_id + "'"
cursor = self.cursor
conn = self.conn
try:
cursor.execute(sql)
except Exception,ex:
print Exception,"line86:",ex
return
if cursor.fetchone(): return #存在数据
#分析数据
s = SeedAnalyse()
seed = s.analyse(hash_id)
if not seed: return
info = seed['info']
name = info['name']
sql = "INSERT INTO bt(hash,name) values (%s,%s)"
param = (hash_id, name )
try:
cursor.execute(sql,param)
conn.commit()
except Exception,ex:
print Exception,":",ex
return
sql = "select * from bt where hash='" + hash_id + "'"
cursor.execute(sql)
nid = cursor.fetchone()[0]
conn.commit()
lenght = 0
if 'files' in info:
for nfile in info['files']:
lenght = nfile['length']
name = nfile['path']
sql = "INSERT INTO file(name,size,btid) values (%s,%s,%s)"
param = ( name, str(lenght), str(nid))
try:
cursor.execute(sql,param)
conn.commit()
except Exception,ex:
print Exception,":",ex
return
else:
lenght = info['length']
sql = "INSERT INTO file(name,size,btid) values (%s,%s,%s)"
param = ( name, str(lenght), str(nid))
try:
cursor.execute(sql,param)
conn.commit()
except Exception,ex:
print Exception,":",ex
return
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/superdebug/pydht.git
git@gitee.com:superdebug/pydht.git
superdebug
pydht
pydht
master

搜索帮助