+def getMyIP(family, config, methods = {}, verbose = False):
+ '''Returns our current IP address (<family> can be "IPv4" or "IPv6"), detected as given by the configuration.
+ Additional detection methods can be supplied via <methods>.'''
+ method = config[family]['method']
+ if method == 'none':
+ return None
+ elif method == 'web':
+ server = config[family].get('server', config['DEFAULT']['server'])
+ ip = urlopen('https://'+server+'/checkip')
+ if verbose:
+ print("Server",server,"says my",family,"is",ip)
+ return ip
+ elif method in 'methods':
+ return methods[method]()
+ else:
+ raise Exception("Unsupported "+family+" detection method: "+method)
+
+def getMyIPv4(config, verbose = False):
+ '''Returns our current IPv4 address, detected as given by the configuration'''
+ return getMyIP("IPv4", config, verbose=verbose)
+
+def getMyIPv6(config, verbose = False):
+ '''Returns our current IPv6 address, detected as given by the configuration'''
+ return getMyIP("IPv6", config, verbose=verbose)