-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket-server.py
150 lines (133 loc) · 4.29 KB
/
socket-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
import socket, psutil, pickle, os, time, netifaces, platform, cpuinfo
from random import randrange
# Cria o socket
socket_servidor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Obtem o nome da máquina
HOST = socket.gethostbyname("")
PORT = 9009
BUFFER_SIZE = 1024
# Associa a PORT
socket_servidor.bind((HOST, PORT))
# Escutando...
socket_servidor.listen()
print("Servidor de nome", HOST, "esperando conexão na porta", PORT)
# Aceita alguma conexão
(socket_cliente,addr) = socket_servidor.accept()
print("Conectado a:", str(addr))
def get_processing_info(time):
values = []
processor_name = platform.processor()
values.append(processor_name)
cpu_freq = psutil.cpu_freq().current
values.append(cpu_freq)
cpu_freq_total = psutil.cpu_freq().max
values.append(cpu_freq_total)
cpu_count = psutil.cpu_count()
values.append(cpu_count)
cpu_count_logical = psutil.cpu_count(logical=False)
values.append(cpu_count_logical)
cpu_percent_values = []
for i in range(time):
cpu_percent = psutil.cpu_percent(interval=1)
cpu_percent_values.append(cpu_percent)
values.append(cpu_percent_values)
info = cpuinfo.get_cpu_info()
cpu_architecture = info['arch']
cpu_name = info['brand_raw']
cpu_word = info['bits']
values.append(cpu_architecture)
values.append(cpu_name)
values.append(cpu_word)
return values
def get_memory_data():
informations = []
mem = psutil.virtual_memory()
memory_capacity = round(mem.total/(1024*1024*1024), 2) # convert to GB
used_memory = round(mem.used/(1024*1024*1024), 2) # convert to GB
percent_memory = mem.percent
informations.append(memory_capacity)
informations.append(used_memory)
informations.append(percent_memory)
return informations
def get_disk_info():
values = []
disk = psutil.disk_usage('.')
total = round(disk.total/(1024*1024*1024), 2) # convert to GB
in_use = round(disk.used/(1024*1024*1024), 2) # convert to GB
free = round(disk.free/(1024*1024*1024), 2) # convert to GB
percent_disk = disk.percent
return (total, in_use, free, percent_disk)
def get_files_and_directories():
list_of_data = os.listdir()
dic_arq = {}
directories = []
files = []
for i in list_of_data:
if os.path.isfile(i):
ext = os.path.splitext(i)[1]
if not ext in dic_arq:
dic_arq[ext] = []
dic_arq[ext].append(i)
else:
directories.append(i)
for i in dic_arq:
for j in dic_arq[i]:
files.append(j)
return (files, directories)
def get_process_data():
process = [proc.name()for proc in psutil.process_iter()]
return process[:10]
def network_info():
info = psutil.net_if_addrs()
ip = info['en1'][0][1]
gateway = netifaces.gateways()
gateway = gateway['default'][2][0]
subnet_mask = info['en1'][0][2]
return (ip, gateway, subnet_mask)
def get_subnetwork_info():
data = []
for i in range(8):
host = f"192.168.0.{i}"
port = randrange(10, 1000)
info = [host, "tcp", port, "open"]
data.append(info)
return data
def get_response():
info_bytes = socket_cliente.recv(BUFFER_SIZE)
list_of_data = pickle.loads(info_bytes)
return list_of_data
def send_response(result):
bytes_resp = pickle.dumps(result)
socket_cliente.send(bytes_resp)
while True:
list_of_information = get_response()
if len(list_of_information) > 1:
option = int(list_of_information[0])
time = int(list_of_information[1])
option = int(list_of_information[0])
if option == 0:
break
if option == 1:
result = get_processing_info(time)
send_response(result)
elif option == 2:
result = get_memory_data()
send_response(result)
elif option == 3:
result = get_disk_info()
send_response(result)
elif option == 4:
result = get_files_and_directories()
send_response(result)
elif option == 5:
result = get_process_data()
send_response(result)
elif option == 6:
result = network_info()
send_response(result)
elif option == 7:
result = get_subnetwork_info()
send_response(result)
# Fecha socket do servidor e cliente
socket_cliente.close()
socket_servidor.close()