Improve ipv4 validation

- Use builtin ipaddress module for validation
- Ensure to exclude the whole loopback network
This commit is contained in:
Mathias Petermann 2022-02-25 11:20:54 +01:00
parent 233af09bd4
commit 2fd4031e21

View File

@ -3,7 +3,7 @@
import json
import re
import socket
import ipaddress
from collections import defaultdict
import requests
@ -89,12 +89,9 @@ class Discovery():
def validate_ipv4(address: object) -> object:
try:
# IP address validation
if socket.inet_aton(address):
# Ignore localhost
if address != "127.0.0.1":
return address
except socket.error:
if ipaddress.ip_address(address) not in ipaddress.ip_network("127.0.0.0/8"):
return address
except ValueError:
return False
address = False