我在下面有这个脚本如何查看域名的国家/地区。我想把每个国家/地区的代码放在一个特定的文件中。
例如:
FR ==> FR.txt
美国==> US.TXT
脚本输出如下:
脚本:
#!/usr/bin/python
import socket
from geolite2 import geolite2
def origin(ip, domain_str, result):
print("{0} [{1}]: {2}".format(domain_str.strip(), ip, result))
def getip(domain_str):
ip = socket.gethostbyname(domain_str.strip())
reader = geolite2.reader()
output = reader.get(ip)
result = output['country']['iso_code']
origin(ip, domain_str, result)
with open("hostnames.txt", "r") as ins:
for domain_str in ins:
try:
getip(domain_str)
except socket.error as msg:
print("{0} [could not resolve]".format(domain_str.strip()))
if len(domain_str) > 2:
subdomain = domain_str.split('.', 1)[1]
try:
getip(subdomain)
except:
continue
geolite2.close()
感谢您提供的任何帮助
答案 0 :(得分:0)
def origin(ip, domain_str, result):
file_name = "{}.txt".format(result)
with open(file_name, 'a') as f:
f.write("{}".format(domain_str.strip()))