Last active
May 28, 2018 20:33
-
-
Save James-E-A/a720f6ce4764d2934f306b4f458f201c to your computer and use it in GitHub Desktop.
Quickly parse [http[s]://][username[:password]@]hostname[:port][/path]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [http[s]://][username[:password]@]hostname[:port][/path] | |
# from username_parse import parse as parse_u | |
# EXAMPLE USAGE: | |
# username,hostname,password,port,path,protocol = parse_u(argv[1]) | |
# username = parse_u(argv[1])[0] | |
# ip,password = parse_u(argv[1])[1:3] | |
# host,port = parse_u(argv[1])[1:4:2] | |
# Not intended for use without a hostname | |
#Conceptual Lambda (vestigial, | |
# but uncommented in case you want to import it): | |
s=lambda h,c='@',r=1: getattr(h, 'rsplit' if r else 'split')(c) | |
def parse(h,username=None,password=None,protocol=None,port=None,path=None): | |
if '://' in h[:10]: #Change 10 to 8 if you want | |
protocol,h=h.split('://',1) | |
if '@' in h: | |
username,h=h.rsplit('@',1) | |
if ':' in username: | |
username,password=username.split(':',1) | |
# print( "\x1b[31;1mInsecure password entry method\x1b[0m") | |
# raise DeprecationWarning("Insecure password entry method") | |
if '/' in h: | |
h,path=h.split('/',1) | |
path='/'+path | |
if ':' in h: | |
h,port=h.split(':') | |
port=int(port) | |
# if protocol and not port: | |
# port=(80 if protocol=='http' else 443 if protocol=='https' else None) | |
# elif port and not protocol: | |
# protocol=('http' if port==80 else 'https' if port==443 else None) | |
return username,h,password,port,path,protocol |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment