From 4c90b19e350198cc4fac15c4df265efae61078f0 Mon Sep 17 00:00:00 2001 From: Vratislav Podzimek Date: Fri, 8 Apr 2022 16:42:44 +0200 Subject: [PATCH] Support specifying User-Agent for downloads in an env var So that we can distinguish CI downloads from real downloads. Ticket: ENT-7919 Changelog: CFBS_USER_AGENT environment variable can now be used to set User-Agent for file downloads --- cfbs/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cfbs/utils.py b/cfbs/utils.py index a9e9b584..2bff8ac2 100644 --- a/cfbs/utils.py +++ b/cfbs/utils.py @@ -209,9 +209,14 @@ def fetch_url(url, target, checksum=None): else: sha = hashlib.sha1() + headers = dict() + user_agent = os.environ.get("CFBS_USER_AGENT") + if user_agent is not None: + headers["User-Agent"] = user_agent + request = urllib.request.Request(url, headers=headers) try: with open(target, "wb") as f: - with urllib.request.urlopen(url) as u: + with urllib.request.urlopen(request) as u: if not (200 <= u.status <= 300): raise FetchError("Failed to fetch '%s': %s" % (url, u.reason)) done = False