-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.py
65 lines (56 loc) · 2.53 KB
/
report.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
from sys import getsizeof
from requests import post
def pretty_print_dict(dictionary: dict) -> str:
"""
Форматированная строка из словаря для отчёта
:param dictionary: Словарь, который необходимо распечатать
:return: Строка, пригодная для печати
"""
pretty_string = ""
for k, v in dictionary.items():
pretty_string += f"\t{k:<20} countReq={v}\n"
pretty_string += "\n"
return pretty_string
def send_note_to_xpaste(note_date: str) -> str:
"""
Отправить заметку на xpaste.pro
:param note_date: Текст заметки
:return Ссылка на заметку
"""
url = "https://xpaste.pro/paste"
payload = {
"language": "text",
"body": f"{note_date}",
"ttl_days": "14",
}
return post(url, data=payload).text
def gen_report_into_file(ip_data_dict: dict, report_file_path: str):
"""
Формарование отчёта в файл
:param report_file_path: Путь к файлу отчёта
:param ip_data_dict: Данные для отчёта - словарь с подсетями, ip адресами и странами
"""
with open(report_file_path, "w") as report_file:
for subnet, ips in ip_data_dict.items():
total_count_req = sum(ips.values())
uniq_ip = len(ips)
report_file.write(f"{subnet} countReq={total_count_req} uniqIp={uniq_ip}\n")
report_file.write(pretty_print_dict(ips))
def gen_report_on_xpaste(ip_data_dict: dict) -> list:
"""
Формарование отчёта на xpaste.pro
:param ip_data_dict: Данные для отчёта - словарь с подсетями, ip адресами и странами
:return: Список ссылок на заметки
"""
limin_of_size_note = 512 * 1000 # Лимит размера заметки на xpaste.pro 512 килобайт
note_data = ""
links_to_notes = []
for subnet, ips in ip_data_dict.items():
total_count_req = sum(ips.values())
uniq_ip = len(ips)
note_data += f"{subnet} countReq={total_count_req} uniqIp={uniq_ip}\n"
# Если размер строки достиг 512 Килобайт, то вставить строку на xpaste.org
if getsizeof(note_data) >= limin_of_size_note:
links_to_notes.append(send_note_to_xpaste(note_data))
note_data = ""
return links_to_notes