diff --git a/cfg/ls336_cfg.py b/cfg/ls336_cfg.py index cb328a4..0b857bb 100644 --- a/cfg/ls336_cfg.py +++ b/cfg/ls336_cfg.py @@ -1,10 +1,15 @@ +from os import environ + +# either change the uri or set the environment variable 'LS_URI' +lakeshore_uri = environ.get('LS_URI', 'tcp://:7777') + Node('example_cryo.psi.ch', # a globally unique identification 'this is an example cryostat for the Frappy tutorial', # describes the node interface='tcp://10767') # you might choose any port number > 1024 Mod('io', # the name of the module 'frappy_demo.lakeshore.LakeshoreIO', # the class used for communication 'communication to main controller', # a description - uri='tcp://129.129.138.78:7777', # the serial connection + uri=lakeshore_uri, # the serial connection ) Mod('T', 'frappy_demo.lakeshore.TemperatureLoop', diff --git a/frappy/lib/__init__.py b/frappy/lib/__init__.py index ffe0b70..7a005b3 100644 --- a/frappy/lib/__init__.py +++ b/frappy/lib/__init__.py @@ -21,7 +21,7 @@ # ***************************************************************************** """Define helpers""" -import os +import re import importlib import linecache import socket @@ -296,23 +296,31 @@ def formatException(cut=0, exc_info=None, verbose=False): return ''.join(res) +HOSTNAMEPAT = re.compile(r'[a-z0-9_.-]+$', re.IGNORECASE) # roughly checking for a valid hostname or ip address + + def parseHostPort(host, defaultport): """Parse host[:port] string and tuples Specify 'host[:port]' or a (host, port) tuple for the mandatory argument. If the port specification is missing, the value of the defaultport is used. - """ - if isinstance(host, (tuple, list)): - host, port = host - elif ':' in host: - host, port = host.rsplit(':', 1) - port = int(port) + raises TypeError in case host is neither a string nor an iterable + raises ValueError in other cases of invalid arguments + """ + if isinstance(host, str): + host, sep, port = host.partition(':') + if sep: + port = int(port) + else: + port = defaultport else: - port = defaultport - assert 0 < port < 65536 - assert ':' not in host - return host, port + host, port = host + if not HOSTNAMEPAT.match(host): + raise ValueError('illegal host name %r' % host) + if 0 < port < 65536: + return host, port + raise ValueError('illegal port number: %r' % port) def tcpSocket(host, defaultport, timeout=None): diff --git a/frappy/lib/asynconn.py b/frappy/lib/asynconn.py index 97cfc37..2aae5da 100644 --- a/frappy/lib/asynconn.py +++ b/frappy/lib/asynconn.py @@ -52,6 +52,7 @@ class AsynConn: scheme = None SCHEME_MAP = {} connection = None # is not None, if connected + HOSTNAMEPAT = re.compile(r'[a-z0-9_.-]+$', re.IGNORECASE) # roughly checking if it is a valid hostname def __new__(cls, uri, end_of_line=b'\n', default_settings=None): scheme = uri.split('://')[0] @@ -59,17 +60,16 @@ class AsynConn: if not iocls: # try tcp, if scheme not given try: - host_port = parseHostPort(uri, None) - except (ValueError, TypeError, AssertionError): + parseHostPort(uri, 1) # check hostname only + except ValueError: if 'COM' in uri: raise ValueError("the correct uri for a COM port is: " - "'serial://COM[?