1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

add support for user:password syntax in urls to get_url

This commit is contained in:
igor 2013-01-24 16:16:23 +01:00 committed by Michael DeHaan
parent 634d59c97f
commit d683c2431e

View file

@ -100,6 +100,29 @@ def url_do_get(module, url, dest):
USERAGENT = 'ansible-httpget' USERAGENT = 'ansible-httpget'
info = dict(url=url, dest=dest) info = dict(url=url, dest=dest)
r = None r = None
parsed = urlparse.urlparse(url)
if '@' in parsed.netloc:
credentials = parsed.netloc.split('@')[0]
if ':' in credentials:
username, password = credentials.split(':')
netloc = parsed.netloc.split('@')[1]
parsed = list(parsed)
parsed[1] = netloc
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
# this creates a password manager
passman.add_password(None, netloc, username, password)
# because we have put None at the start it will always
# use this username/password combination for urls
# for which `theurl` is a super-url
authhandler = urllib2.HTTPBasicAuthHandler(passman)
# create the AuthHandler
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
#reconstruct url without credentials
url = urlparse.urlunparse(parsed)
request = urllib2.Request(url) request = urllib2.Request(url)
request.add_header('User-agent', USERAGENT) request.add_header('User-agent', USERAGENT)