diff --git a/get_info_from_ripe.py b/get_info_from_ripe.py index a360616..111ee57 100755 --- a/get_info_from_ripe.py +++ b/get_info_from_ripe.py @@ -2,34 +2,9 @@ import argparse import requests -import socket +from pylib.whois import whois_query url = "https://stat.ripe.net/data/country-resource-list/data.json?resource=RU&v4_format=prefix" -whois_server = "whois.ripe.net" - -def whois_query(whois_server, query, get_field="netname"): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((whois_server, 43)) - - query = f"{query}\r\n" - s.send(query.encode()) - - response = '' - while True: - data = s.recv(4096) - try: - response += data.decode('utf-8') - except: - response += data.decode('latin-1') - if not data: - break - s.close() - - for line in response.split('\n'): - if line.startswith(get_field + ':'): - return line.strip() - - return None def get_data(json, file, attr, field, prefix=""): limit = args.limit @@ -41,7 +16,7 @@ def get_data(json, file, attr, field, prefix=""): if count > limit: response = None else: - response = whois_query(whois_server, x, field) + response = whois_query(x, field) if response is None: name = "-no-description-" else: diff --git a/network_list_from_netname.py b/network_list_from_netname.py index db42e95..0e4e7f4 100755 --- a/network_list_from_netname.py +++ b/network_list_from_netname.py @@ -1,36 +1,10 @@ #!/usr/bin/env python3 -import socket import argparse import requests import ipaddress import re - -whois_server = "whois.ripe.net" - -def whois_query(whois_server, query, get_field="netname"): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((whois_server, 43)) - - query = f"{query}\r\n" - s.send(query.encode()) - - response = '' - while True: - data = s.recv(4096) - try: - response += data.decode('utf-8') - except: - response += data.decode('latin-1') - if not data: - break - s.close() - - for line in response.split('\n'): - if line.startswith(get_field + ':'): - return line.strip() - - return None +from pylib.whois import whois_query def convert_to_raw_github_url(url): return url.replace("https://github.com/", "https://raw.githubusercontent.com/").replace("/blob", "") @@ -55,7 +29,7 @@ def extract_netname(filename_or_url): for line in lines: if re.match(r'^netname:', line): netname = line.split(':')[1].strip() - response = whois_query(whois_server, netname, "inetnum") + response = whois_query(netname, "inetnum") if response is not None: if not args.quiet: print(f"# Network name: {netname}") diff --git a/pylib/__pycache__/whois.cpython-311.pyc b/pylib/__pycache__/whois.cpython-311.pyc new file mode 100644 index 0000000..398693f Binary files /dev/null and b/pylib/__pycache__/whois.cpython-311.pyc differ diff --git a/pylib/whois.py b/pylib/whois.py new file mode 100755 index 0000000..d36c97b --- /dev/null +++ b/pylib/whois.py @@ -0,0 +1,28 @@ +import socket + +def whois_query(query, get_field="netname"): + + whois_server = "whois.ripe.net" + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((whois_server, 43)) + + query = f"{query}\r\n" + s.send(query.encode()) + + response = '' + while True: + data = s.recv(4096) + try: + response += data.decode('utf-8') + except: + response += data.decode('latin-1') + if not data: + break + s.close() + + for line in response.split('\n'): + if line.startswith(get_field + ':'): + return line.strip() + + return None