add normalize_uri instead of short_hostname
- use gethostbyaddr only in case of numeric IP - hard wired reverse CNAME (network alias) for linse-c
This commit is contained in:
35
normalizeuri.py
Normal file
35
normalizeuri.py
Normal file
@ -0,0 +1,35 @@
|
||||
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}'
|
||||
|
||||
|
Reference in New Issue
Block a user