-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
98 lines (77 loc) · 2.24 KB
/
client.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
import threading
import json
import socket
import Queue
class QueryThread(threading.Thread):
def __init__(self, command, host, port, logfile_name, queue):
super(QueryThread, self).__init__()
self.command = command + ' ' + logfile_name
self.host = host
self.port = port
self.queue = queue
self.result = ''
self.logfile_name = logfile_name
def run(self):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1.0)
sock.connect((self.host, int(self.port)))
except socket.error as e: #If connection to remote machine fails
print 'Could not connect to ' + str(self.host)
return
try:
sock.sendall(self.command)
while True:
part = sock.recv(10240000)
if part:
self.result += part
else:
if self.result:
self.queue.put({self.logfile_name: self.result})
break
except socket.error as e:
return
class Client(object):
def __init__(self, command, is_test):
self.command = command
self.is_test = is_test
with open('config.json') as config:
config_data = json.load(config)
self.servers = config_data['servers']
self.port = config_data['port']
self.queue = Queue.Queue()
def query(self):
thread_list = []
output = ''
for server in self.servers:
host = server['host']
if self.is_test:
thread = QueryThread(self.command, host, self.port, server['test_logfile_name'], self.queue)
else:
thread = QueryThread(self.command, host, self.port, server['logfile_name'], self.queue)
thread.start()
thread_list.append(thread)
for thread in thread_list:
thread.join()
lines_matched = []
while not self.queue.empty():
item = self.queue.get()
file_name, result = item.keys()[0], item.values()[0]
output += (result + '\n')
if not self.is_test:
print 'File name: ' + (file_name)
print result
lines_matched.append('File name: ' + file_name + ', Number of lines: ' + str(result.count('\n') + 1))
print ''
if not self.is_test:
for line in lines_matched:
print line
print 'Total lines matched:%s' % (output.count('\n'))
return output
def main():
while True:
command = raw_input('\nEnter grep command...\n')
obj = Client(command, is_test = False)
obj.query()
if __name__ == '__main__':
main()