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

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