Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def safe_urlsplit(url, scheme='http'):
if isinstance(url, SplitResult):
return url
if not re.match(PROTOCOL_RE, url):
url = scheme + '://' + url
splitted = urlsplit(url)
return splitted
Args:
url (str): Target URL as a string.
protocol (str): protocol wanted. Is 'http' by default.
Returns:
string: The protocol-equipped url.
"""
protocol = protocol.rstrip(':/')
if not PROTOCOL_RE.match(url):
url = protocol + '://' + url
elif url[:2] == '//':
url = protocol + ':' + url
else:
url = re.sub(PROTOCOL_RE, protocol + '://', url)
return url
def strip_protocol(url):
"""
Function removing the protocol from the given url.
Args:
url (str): Target URL as a string.
Returns:
string: The url without protocol.
"""
return PROTOCOL_RE.sub("", url)