diff options
author | Alexander Sulfrian <asulfrian@zedat.fu-berlin.de> | 2022-01-16 19:59:25 +0100 |
---|---|---|
committer | Alexander Sulfrian <asulfrian@zedat.fu-berlin.de> | 2022-01-23 19:57:46 +0100 |
commit | 9cef2774875594d91d73a4cbc3cd6935bc992cec (patch) | |
tree | 547bbdf558532c52e56658f67a70dc11d6b2d633 /src/lib/Bcfg2/Server | |
parent | 8d0683c366afc6c5c6db39ea722fef37daa5ea6d (diff) | |
download | bcfg2-9cef2774875594d91d73a4cbc3cd6935bc992cec.tar.gz bcfg2-9cef2774875594d91d73a4cbc3cd6935bc992cec.tar.bz2 bcfg2-9cef2774875594d91d73a4cbc3cd6935bc992cec.zip |
Packages: Add possibility to customize User-Agent
Some mirror might block the default python urllib User-Agent.
Diffstat (limited to 'src/lib/Bcfg2/Server')
-rw-r--r-- | src/lib/Bcfg2/Server/Plugins/Packages/Source.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/lib/Bcfg2/Server/Plugins/Packages/Source.py b/src/lib/Bcfg2/Server/Plugins/Packages/Source.py index 9c9c9c5bb..b0c4bf44f 100644 --- a/src/lib/Bcfg2/Server/Plugins/Packages/Source.py +++ b/src/lib/Bcfg2/Server/Plugins/Packages/Source.py @@ -57,7 +57,7 @@ from Bcfg2.Compat import HTTPError, HTTPBasicAuthHandler, \ from Bcfg2.Server.Statistics import track_statistics -def fetch_url(url): +def fetch_url(url, opts): """ Return the content of the given URL. :param url: The URL to fetch content from. @@ -74,8 +74,14 @@ def fetch_url(url): url = mobj.group(1) + mobj.group(4) auth = HTTPBasicAuthHandler(HTTPPasswordMgrWithDefaultRealm()) auth.add_password(None, url, user, passwd) - install_opener(build_opener(auth)) - return urlopen(url).read() + req = build_opener(auth) + else: + req = build_opener() + + if 'user-agent' in opts: + req.addheaders = [('User-Agent', opts['user-agent'])] + + return req.open(url).read() class SourceInitError(Exception): @@ -735,7 +741,7 @@ class Source(Debuggable): # pylint: disable=R0902 self.logger.info("Packages: Updating %s" % url) fname = self.escape_url(url) try: - open(fname, 'wb').write(fetch_url(url)) + open(fname, 'wb').write(fetch_url(url, self.server_options)) except ValueError: self.logger.error("Packages: Bad url string %s" % url) raise |