From ee30ff730f9b7f7b8383a8c2f3a7a19c343d67e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Mon, 17 Dec 2018 19:56:03 +0100 Subject: [PATCH] fix: saner way to test if a string is an IP --- urlabuse/urlabuse.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/urlabuse/urlabuse.py b/urlabuse/urlabuse.py index 1c72852..3871189 100644 --- a/urlabuse/urlabuse.py +++ b/urlabuse/urlabuse.py @@ -10,6 +10,7 @@ import json import redis from urllib.parse import quote from .helpers import get_socket_path +import ipaddress from pyfaup.faup import Faup @@ -128,24 +129,11 @@ def is_valid_url(url): def is_ip(host): - if isinstance(host, bytes): - to_search = b':' - else: - to_search = ':' - - if to_search in host: - try: - socket.inet_pton(socket.AF_INET6, host) - return True - except Exception: - pass - else: - try: - socket.inet_aton(host) - return True - except Exception: - pass - return False + try: + ipaddress.ip_address(host) + return True + except ValueError: + return False def try_resolve(fex, url):