summaryrefslogtreecommitdiffstats
path: root/ipapython/ipautil.py
diff options
context:
space:
mode:
authorJan Cholasta <jcholast@redhat.com>2011-09-30 10:09:55 +0200
committerMartin Kosek <mkosek@redhat.com>2011-10-05 10:58:25 +0200
commit12bfed37d4d22319e2cfadb5d9b460da7e748432 (patch)
tree58deb5415d489ac25bad5df267a2df523ba142cc /ipapython/ipautil.py
parenta16b5b4c00ca9b82cd40a2c2be22c9e77e0ce64a (diff)
downloadfreeipa-12bfed37d4d22319e2cfadb5d9b460da7e748432.tar.gz
freeipa-12bfed37d4d22319e2cfadb5d9b460da7e748432.tar.xz
freeipa-12bfed37d4d22319e2cfadb5d9b460da7e748432.zip
Add a function for formatting network locations of the form host:port for use in URLs.
If the host part is a literal IPv6 address, it must be enclosed in square brackets (RFC 2732). ticket 1869
Diffstat (limited to 'ipapython/ipautil.py')
-rw-r--r--ipapython/ipautil.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index cfc979ed..dfeaa9e0 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -151,6 +151,24 @@ class CheckedIPAddress(netaddr.IPAddress):
def valid_ip(addr):
return netaddr.valid_ipv4(addr) or netaddr.valid_ipv6(addr)
+def format_netloc(host, port=None):
+ """
+ Format network location (host:port).
+
+ If the host part is a literal IPv6 address, it must be enclosed in square
+ brackets (RFC 2732).
+ """
+ host = str(host)
+ try:
+ socket.inet_pton(socket.AF_INET6, host)
+ host = '[%s]' % host
+ except socket.error:
+ pass
+ if port is None:
+ return host
+ else:
+ return '%s:%s' % (host, str(port))
+
def realm_to_suffix(realm_name):
s = realm_name.split(".")
terms = ["dc=" + x.lower() for x in s]