Use getaddrinfo to detect IP literals in client example

socket.inet_pton() is missing on some supported platforms (Python 2.7
on Windows). getaddrinfo() with AI_NUMERICHOST parses without
resolving and is available everywhere. It also handles scoped IPv6
literals.
pull/70/head
Juliusz Sosinowicz 2026-07-13 17:53:14 +00:00
parent 0446e8c73f
commit 89f038ab7c
1 changed files with 7 additions and 7 deletions

View File

@ -132,13 +132,13 @@ def get_DTLSmethod(index):
)[index]
def is_ip_literal(host):
for family in (socket.AF_INET, socket.AF_INET6):
try:
socket.inet_pton(family, host)
return True
except (socket.error, ValueError):
pass
return False
# AI_NUMERICHOST never resolves, it only parses. Unlike
# socket.inet_pton() it is available on every supported platform.
try:
socket.getaddrinfo(host, None, 0, 0, 0, socket.AI_NUMERICHOST)
return True
except socket.error:
return False
def configure_verification(context, args):