-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (76 loc) · 2.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import bencodepy
import codecs
import socket
import threading
from btdht import btdht
from fetchMetadata import fetch_metadata
from queue import Queue
from time import sleep
from sqlalchemy import create_engine, Table, String, MetaData, Column
from sqlalchemy.sql import select
metadata = MetaData()
hash_tab = Table("hash_tab", metadata,
Column("infohash", String(40), primary_key=True),
Column("name", String(128)),
)
class Master(threading.Thread):
def __init__(self):
super().__init__()
self.setDaemon(True)
self.que = Queue()
self.conn = create_engine("postgresql://gaoyb7@localhost/dht_demo").connect()
self.ins = hash_tab.insert()
def log_in_database_demo(self, infohash, name):
try:
self.conn.execute(self.ins, infohash=infohash, name=name)
except Exception as e:
pass
def logger(self):
while True:
if self.que.empty():
sleep(1)
continue
else:
r = self.que.get()
self.log_in_database_demo(r[1], r[2])
def run(self):
#while True:
# self.fetch()
dt = threading.Thread(target=self.logger)
dt.setDaemon(True)
dt.start()
while True:
if threading.activeCount() < 1500:
if self.que.qsize() == 0:
sleep(1)
continue
r = self.que.get()
t = threading.Thread(target=fetch_metadata, args=(r[0], r[1], r[2]))
t.setDaemon(True)
t.start()
else:
sleep(1)
def fetch(self):
for i in range(100):
if self.que.qsize() == 0:
sleep(1)
continue
r = self.que.get()
t = threading.Thread(target=fetch_metadata, args=(r[0], r[1], r[2]))
t.setDaemon(True)
t.start()
def log(self, nid, infohash, name, address):
#print("%s %s" % (codecs.encode(infohash, "hex_codec").decode(), name.decode("utf-8")))
#fetch_metadata(nid, infohash, address)
#print(self.que.qsize())
if self.que.qsize() > 5000:
sleep(1)
self.que.put([nid, codecs.encode(infohash, "hex_codec").decode(), name.decode()])
#print(self.que.qsize())
#print(infohash, self.que.qsize(), threading.activeCount())
if __name__ == "__main__":
master = Master()
master.start()
dht = btdht(master, "0.0.0.0", 6881, 100)
dht.start()
dht.auto_send_find_node()