Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85c7cad

Browse files
committed
build_parameters.py: use http session
1 parent 5094ffc commit 85c7cad

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

build_parameters.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
import shutil # noqa: F401
3535
import sys
3636
import time # noqa: F401
37-
import urllib.request
3837
from html.parser import HTMLParser
3938

39+
import requests
40+
4041
parser = argparse.ArgumentParser(description="python3 build_parameters.py [options]")
4142
parser.add_argument("--verbose", dest='verbose', action='store_false', default=True, help="show debugging output")
4243
parser.add_argument("--ardupilotRepoFolder", dest='gitFolder', default="../ardupilot", help="Ardupilot git folder. ")
@@ -73,6 +74,14 @@ def format(self, record):
7374
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO, handlers=[handler])
7475
logger = logging.getLogger(__name__)
7576

77+
# Global session for HTTP requests with connection pooling
78+
session = requests.Session()
79+
session.headers.update({
80+
'User-Agent': 'Mozilla/5.0 (compatible; ArduPilotWikiBuilder/1.0)',
81+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
82+
'Connection': 'keep-alive'
83+
})
84+
7685
# Parameters
7786
COMMITFILE = "git-version.txt"
7887
BASEURL = "https://firmware.ardupilot.org/"
@@ -274,7 +283,11 @@ def handle_starttag(self, tag, attrs):
274283
html_parser = ParseText()
275284
try:
276285
debug(f"Fetching {firmware_url}{vehicle}")
277-
html_parser.feed(urllib.request.urlopen(firmware_url + vehicle).read().decode('utf8'))
286+
287+
response = session.get(firmware_url + vehicle, timeout=30)
288+
response.raise_for_status()
289+
content = response.text
290+
html_parser.feed(content)
278291
except Exception as e:
279292
error(f"Vehicles folders list download error: {e}")
280293
sys.exit(1)
@@ -324,7 +337,11 @@ def handle_starttag(self, tag, attrs):
324337
html_parser = ParseText()
325338
try:
326339
debug(f"Fetching {url}")
327-
html_parser.feed(urllib.request.urlopen(url).read().decode('utf8'))
340+
341+
response = session.get(url, timeout=30)
342+
response.raise_for_status()
343+
content = response.text
344+
html_parser.feed(content)
328345
except Exception as e:
329346
error(f"Board folders list download error: {e}")
330347
finally:
@@ -344,9 +361,9 @@ def fetch_commit_hash(version_link, board, file):
344361
progress(f"Processing link...\t{fetch_link}")
345362

346363
try:
347-
fecth_response = ""
348-
with urllib.request.urlopen(fetch_link) as response:
349-
fecth_response = response.read().decode("utf-8")
364+
response = session.get(fetch_link, timeout=30)
365+
response.raise_for_status()
366+
fecth_response = response.text
350367

351368
commit_details = fecth_response.split("\n")
352369
commit_hash = commit_details[0][7:]

0 commit comments

Comments
 (0)