'Returns host or host:port or empty string from a given url or host
If no port is found and default_port is given use host:default_port'

def clean_host(self, host, default_port=None):

    host = host.strip()
    if host:
        match_host_port = re.search(u'(?:http.*://)?(?P<host>[^:/]+).?(?P<port>[0-9]*).*', host)
        cleaned_host = match_host_port.group(u'host')
        cleaned_port = match_host_port.group(u'port')
        if cleaned_host:
            if cleaned_port:
                host = ((cleaned_host + u':') + cleaned_port)
            elif default_port:
                host = ((cleaned_host + u':') + str(default_port))
            else:
                host = cleaned_host
        else:
            host = u''
    return host
