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:
2025-05-26 08:55:11 +02:00
parent ef35d18f37
commit e735ebe5f9
3 changed files with 40 additions and 19 deletions

35
normalizeuri.py Normal file
View 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}'

View File

@ -7,6 +7,7 @@ import logging
from collections import namedtuple from collections import namedtuple
from select import select from select import select
from streams import Stream, Base, StreamDead from streams import Stream, Base, StreamDead
from normalizeuri import normalizeuri
IDN = re.compile('.*ISSE.*,SEC[oO]P,') IDN = re.compile('.*ISSE.*,SEC[oO]P,')
DESCRIBING = re.compile(r'describing \S* (.*)$') DESCRIBING = re.compile(r'describing \S* (.*)$')
@ -139,7 +140,7 @@ class UdpStream(Base):
# msg['device'] = uri.split('://', 1)[-1].split(':')[0] # msg['device'] = uri.split('://', 1)[-1].split(':')[0]
kwargs = msg kwargs = msg
elif kind == 'node': elif kind == 'node':
uri = f"{addr}:{msg['port']}" uri = normalizeuri(f"{addr}:{msg['port']}")
kwargs = {'name': msg['equipment_id']} kwargs = {'name': msg['equipment_id']}
else: else:
continue continue

View File

@ -3,6 +3,7 @@ import time
import re import re
import logging import logging
from select import select from select import select
from normalizeuri import normalizeuri
class StreamDead(Exception): class StreamDead(Exception):
@ -34,20 +35,6 @@ class Base:
logging.info('FINISH BASE') logging.info('FINISH BASE')
def short_hostname(host):
"""psi/lin/se special
- treat case where -129129xxxx is appended
"""
host = socket.gethostbyaddr(host)[0]
if host == 'localhost':
host = socket.gethostname()
match = re.match(r'([^.-]+)(?:-129129\d{6}|(-[~.]*|)).psi.ch', host)
if match:
host = match.group(1) + (match.group(2) or '')
return host
class Stream(Base): class Stream(Base):
_last_time = None _last_time = None
dead = False dead = False
@ -82,10 +69,8 @@ class Stream(Base):
self.socket = socket.create_connection(parse_uri(self.uri)) self.socket = socket.create_connection(parse_uri(self.uri))
self.select_read[self.socket.fileno()] = self self.select_read[self.socket.fileno()] = self
self.settimeout(self.timeout) self.settimeout(self.timeout)
host, _, port = self.uri.partition(':') self.uri = self.tags['stream'] = f'{normalizeuri(self.uri)}'
# try to convert uri to host name logging.info('connected %s', self.uri)
self.uri = self.tags['stream'] = f'{short_hostname(host)}:{port}'
logging.info('connected %s:%s = %s', host, port, self.uri)
self._buffer = [] self._buffer = []
self._deadline = INF self._deadline = INF
self._next_connect = 0 self._next_connect = 0