From 13bb88ef97dfeb01acebecfbf8c1602b55a602c1 Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 24 Jan 2013 16:16:23 +0100 Subject: [PATCH] add support for user:password syntax in urls to get_url --- get_url | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/get_url b/get_url index 933c4445b2b..1cbc4a12a8c 100644 --- a/get_url +++ b/get_url @@ -100,6 +100,29 @@ def url_do_get(module, url, dest): USERAGENT = 'ansible-httpget' info = dict(url=url, dest=dest) 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.add_header('User-agent', USERAGENT)