13 Star 26 Fork 10

元谷 / 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
Python
1
https://gitee.com/yuangu/pydht.git
git@gitee.com:yuangu/pydht.git
yuangu
pydht
pydht
master

搜索帮助