forked from arrdem/OpenSourcerer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
essentialmagic_server.py
155 lines (126 loc) · 4.43 KB
/
essentialmagic_server.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python2
from pymongo.connection import Connection
from ConfigParser import SafeConfigParser
import socket
import threading
import pickle
import re
global parser
parser = SafeConfigParser()
parser.read('settings.ini')
connection = Connection(parser.get('mongodb', 'server'))
global db
db = None
exec("db = connection." + parser.get('mongodb', 'db'))
class connectionThread(threading.Thread):
def __init__(self, conn):
threading.Thread.__init__(self)
self.__conn__ = conn
self.__running__= True
self.__last__ = 0
self.__client__ = socket.gethostbyaddr(self.__conn__.getpeername()[0])[0]
def join(self):
self.__running__ = False
self.__conn__.send("DONE".encode())
print("[ %30s ] Thread killed..." % (self.__client__))
try:
threading.Thread.join(self)
except:
return
def run(self):
global ticker
data = " "
while self.__running__ and data:
data = str(self.__conn__.recv(4096), "utf-8")
#print("[", self.ident, "] UPKEEP SIGNAL FROM", self.__conn__.getpeername())
if(ticker >= limit):
self.__conn__.send(str("DONE").encode())
self.join()
elif data == "NEXT":
ticker += 1
self.__last__ = ticker
self.__conn__.send(str(ticker).encode())
elif "FAIL" in data:
i = int(re.sub("FAIL ", '', data))
#print("[ %30s ] FAILED TO DOWNLOAD DECK ID %i" % (self.__client__, i))
elif "OKAY" in data:
# this is the signal from the client that a new deck has
# been found. Format is as follows:
# DECK (bytes)
# followed by the pickled dict
try:
i = int(re.sub("OKAY ", '', data))
self.__conn__.send('OK'.encode())
except:
continue
deck = None
try:
deck = pickle.loads(self.__conn__.recv(i))
self.__conn__.send('1'.encode())
except:
self.__conn__.send('0'.encode())
if deck is not None:
m = {}
for c in deck:
m[c] = deck[c]
m['sum'] = len(deck)
for c1 in deck:
db.markov.update({'name': c1}, {"$inc": m}, upsert=True)
print("[ %30s ] DOWNLOADED DECK %i" % \
(self.__client__, self.__last__))
if(not data):
print("[ %30s ] NO TRAFFIC, EXITING" % (self.__client__))
class Server(object):
__thread__ = None
__clients__ = []
__host__ = ''
__port__ = 0
__buff__ = 0
__addr__ = ''
__up__ = False
__sock__ = None
__count__ = 0
def __init__(self, host='', port=9001, buff=1024):
self.__host__ = host
self.__port__ = port
self.__buff__ = buff
self.__addr__ = (self.__host__, self.__port__)
self.__up__ = True
self.__sock__ = socket.socket()
self.__sock__.bind(self.__addr__)
self.__sock__.settimeout(1.0)
self.__sock__.listen(2)
def run(self):
if self.__thread__:
return
else:
self.__thread__ = threading.Thread(target=self.__run__)
self.__thread__.start()
def stop(self):
if self.__thread__:
for t in self.__clients__:
t.join()
print("Killed thread", t)
self.__up__ = False
self.__thread__.join()
print("Thread killed...")
else:
print("Server was not running..")
def __run__(self):
print("Awaiting connections. . .")
while self.__up__:
try:
client = self.__sock__.accept()
print("Client connected from {}.".format(client[1]))
self.__clients__.append(connectionThread(client[0]))
self.__clients__[-1].start()
except socket.timeout:
continue
if __name__ == "__main__" or 1:
global ticker, limit
ticker = int(parser.get('essentialmagic', 'ticker'))
limit = int(parser.get('essentialmagic', 'limit'))
serv = Server()
serv.run()
input()
serv.stop()