diff options
-rwxr-xr-x | lorry-controller | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/lorry-controller b/lorry-controller index d9dc1e0..0ae4ceb 100755 --- a/lorry-controller +++ b/lorry-controller @@ -285,10 +285,10 @@ class LorryController(cliapp.Application): self.runcmd(['git']+args, cwd=os.path.join(self.settings['work-area'], 'git')) - def maybe_http_request(self, url, dry=False): + def maybe_http_request(self, url, auth=None, dry=False): """If not a dry run, make an HTTP request and return its output.""" if (not self.settings['dry-run']) or dry: - return self.http_request(url) + return self.http_request(url, auth) else: logging.debug('DRY-RUN: Not sending a request to %s' % url) return 0, 'DRY-RUN', 'DRY-RUN' @@ -300,7 +300,7 @@ class LorryController(cliapp.Application): logging.debug("DRY-RUN: Not running %r" % cmdline) return 0, 'DRY-RUN', 'DRY-RUN' - def http_request(self, url): + def http_request(self, url, auth=None): """Make an HTTP request to the given url, return the output. Make an HTTP request to `url`. If the request succeeds (response code @@ -310,7 +310,15 @@ class LorryController(cliapp.Application): """ request = urllib2.Request(url, None, {}) - response = urllib2.urlopen(request) + if auth: + password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() + password_mgr.add_password( + None, url, auth['username'], auth['password']) + auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr) + opener = urllib2.build_opener(auth_handler) + response = opener.open(url) + else: + response = urllib2.urlopen(request) code = response.getcode() if code == 200: return 0, response.read(), '200' |