Files
sehistory/normalizeuri.py
Markus Zolliker e735ebe5f9 add normalize_uri instead of short_hostname
- use gethostbyaddr only in case of numeric IP
- hard wired reverse CNAME (network alias) for linse-c
2025-05-26 08:55:11 +02:00

36 lines
1.0 KiB
Python

import re
import socket
# sorry for hardwiring this ... there is no CNAME reverse lookup!
# taking the original address as unique name would need a call to
# gethostbyaddr, which might take some time - also not what we want
reverse_alias = {
'pc15139': 'linse-c',
'pc16392': 'linse-a',
}
def normalizeuri(uri):
host, sep, port = uri.partition(':')
if host[0].isdigit():
try:
socket.setdefaulttimeout(1)
host = socket.gethostbyaddr(host)[0].split('.', 1)[0]
except socket.gaierror:
pass # keep numbered IP
finally:
socket.setdefaulttimeout(None)
else:
host = host.split('.',1 )[0]
if host == 'localhost':
host = socket.gethostname()
else:
# this resolves CNAME entries (network aliases):
host = reverse_alias.get(host, host)
# strip appended IP when a host is registered twice (at PSI):
match = re.match(r'([^-]+)-129129\d{6}$', host)
host = match.group(1) if match else host
return f'{host}{sep}{port}'