TypeError: unhashable type: 'list' при вводе данных в JSON
Подскажите, пожалуйста. У меня есть .txt файл с именами доменов. Мне нужно представить результат в формате JSON по типу
{"website.com":[{
"hostname": "www.about.website.com"
"ports": [{
"80": "http"
}],
"hostname": "details.website.com"
"ports": [{
"80": "http",
"443": "HTTPS"
}]
},
{"web.ru": [{
"hostname": "test.web.ru"
}]
}]
}
Мне код выдаёт ошибку
Traceback (most recent call last):
File "C:\Users\PycharmProjects\pythonProject\main.py", line 71, in <module>
'website.com':[{data}]
TypeError: unhashable type: 'list'
указывая на 71-ую строку 'website.com': [{data}]
и в файле hosts.json не тот формат данных
[
{
"hostname": "www.about.website.ru",
"ports": [
{
"80": "http",
}
]
}
][
"hostname": "details.website.com"
"ports": [
{
"80": "http",
"443": "HTTPS"
}
]
][
{
"hostname": "test.web.ru"
}
][
{
"hostname": "test.web.ru",
"ports": [
{}
]
}
]
Вот мой код
import json
import socket
from ping3 import ping
ports = [20, 21, 22, 23, 25, 43, 53, 80,
115, 123, 143, 161, 179, 443, 445,
514, 515, 993, 995, 1080, 1194,
1433, 1723, 3128, 3268, 3306, 3389,
5432, 5060, 5900, 8080, 10000]
services = [
"FTP-DATA", "FTP", "SSH", "Telnet", "SMTP", "WHOIS", "DNS", "http", "SFTP", "NTP", "IMAP", "SNMP", "BGP", "HTTPS",
"MICROSOFT-DS", "SYSLOG", "PRINTER", "IMAPS", "POP3S", "SOCKS", "OpenVPN", "SQL Server", "PPTP", "HTTP", "LDAP",
"MySQL", "RDP", "PostgreSQL", "VNC", "Tomcat", "Webmin"]
numLines = sum(1 for line in open('../pythonProject/list_of_hosts.txt'))
hosts_website = []
hosts_web = []
def scan_and_write(host_name):
host_ping = ping(host_name)
if host_ping is not False:
ip = socket.gethostbyname(host_name)
json_data = [{
"hostname": f"{host_name}",
"ports": [{}]
}]
try:
a, s = 0, None
for a in range(len(ports)):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(0.01)
result = s.connect_ex((ip, ports[a]))
if result == 0:
json_data[0]["ports"][0][ports[a]] = services[a]
with open("hosts.json", "a") as file:
file.write(json.dumps(json_data, indent=2, ensure_ascii=False))
if s is not None:
s.close()
except socket.error:
print("Host not responding")
else:
json_data = [{
"hostname": f"{host_name}"
}]
with open("hosts.json", "a") as file:
file.write(json.dumps(json_data, indent=2, ensure_ascii=False))
with open('../pythonProject/list_of_hosts.txt') as f:
i = 0
for i in range(numLines):
line = f.readline()
if 'website.com' in line:
hosts_websute.append(line.rstrip('\n'))
with open('../pythonProject/list_of_hosts.txt') as f:
i = 0
for i in range(numLines):
line = f.readline()
if 'web' in line:
hosts_web.append(line.rstrip('\n'))
with open('hosts.json', "w") as d:
for i in range(len(hosts_website)):
data = [scan_and_write(host_name=hosts_website[i])]
json_main_website = [{
'website.com': [{data}]
}]
d.write(json.dumps(json_main_website, indent=2))
for i in range(len(hosts_web)):
data = [scan_and_write(host_name=hosts_web[i])]
json_main_lrmru = [{
'web.ru': [{data}]
}]
d.write(json.dumps(json_main_web, indent=2))