diff --git a/pyproject.toml b/pyproject.toml index b52324f..40a995e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.7" pytest = ">=3.6.3" +ipwhois = ">= 1.0.0" [tool.poetry.dev-dependencies] coverage = "^7.0" diff --git a/pytest_socket.py b/pytest_socket.py index cef3eaa..803c835 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -1,5 +1,7 @@ import ipaddress import socket +from ipwhois import IPWhois +from ipwhois.exceptions import HTTPLookupError, IPDefinedError import pytest @@ -16,9 +18,15 @@ class SocketConnectBlockedError(RuntimeError): def __init__(self, allowed, host, *_args, **_kwargs): if allowed: allowed = ",".join(allowed) + try: + ip_whois = IPWhois(host).lookup_rdap(depth=1) + host_description = ip_whois.get("asn_description", None) + except (HTTPLookupError, IPDefinedError): + host_description = None + host_text = f"{host} ({host_description})" if host_description else host super().__init__( "A test tried to use socket.socket.connect() " - f'with host "{host}" (allowed: "{allowed}").' + f'with host "{host_text}" (allowed: "{allowed}").' )