Modem restart: load restarting.ha, which is what starts it
Lint / JS (eslint) (push) Successful in 8s
Lint / Python (flake8) (push) Successful in 1m2s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m3s
Lint / Python (flake8) (pull_request) Successful in 2m7s
Test / Python Tests (pytest) (push) Successful in 2m20s
Lint / JS (eslint) (pull_request) Successful in 15s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 57s
Test / Python Tests (pytest) (pull_request) Successful in 1m28s

Tried on the real BGW320 (firmware 6.40.4): posting the Restart form only
answers 302 to /cgi-bin/restarting.ha, and the modem keeps running (its
uptime carried on). Loading that page, as a browser does, starts the
restart ("restarting ... approximately 155 seconds"). restart() now
follows the redirect and only reports 'accepted' when that page says it
is restarting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-19 20:34:50 -05:00
1 parent c2ff072eb2
commit 3afd85626b
2 files changed
+35 -12

No files matched your search

+15 -6
View File
@@ -394,6 +394,7 @@ _DIAG_PAGE = '/cgi-bin/diag.ha'
_LOGS_PAGE = '/cgi-bin/logs.ha'
_SPEED_PAGE = '/cgi-bin/speed.ha'
_RESTART_PAGE = '/cgi-bin/restart.ha'
_RESTARTING_PAGE = '/cgi-bin/restarting.ha'
# Firewall log row: number, ISO time (gateway's local clock), source, destination, protocol, reason
_FW_ROW_RE = re.compile(r'<tr[^>]*>\s*<td[^>]*>\s*\d+\s*</td>\s*<td>\s*([^<]+?)\s*</td>\s*<td>\s*([^<]*?)\s*</td>'
r'\s*<td>\s*([^<]*?)\s*</td>\s*<td>\s*([^<]*?)\s*</td>\s*<td>\s*([^<]*?)\s*</td>\s*</tr>', re.I)
@@ -540,19 +541,27 @@ class ModemAdmin:
def restart(self) -> str:
"""Restart the gateway from its Restart Device page: the one change this client makes,
and only when an admin asks. Returns 'accepted', or 'unconfirmed' when the gateway
dropped the connection before answering (it may already be going down)."""
and only when an admin asks.
Two steps, as in a browser: posting the form only answers with a redirect to
restarting.ha, and loading that page is what starts the restart (firmware 6.40; a
post alone leaves the gateway running). Returns 'accepted' once that page says it is
restarting, or 'unconfirmed' when the gateway dropped the connection first.
"""
nonce = _NONCE_RE.search(self._page(_RESTART_PAGE))
if not nonce:
raise ModemDataInvalid('Restart page has no nonce')
timeout = max(self.timeout, 30)
try:
resp = self.session.post(f'{self.base_url}{_RESTART_PAGE}', allow_redirects=False,
timeout=max(self.timeout, 30),
data={'nonce': nonce.group(1), 'Restart': 'Restart'})
timeout=timeout, data={'nonce': nonce.group(1), 'Restart': 'Restart'})
if resp.status_code == 302 and _RESTARTING_PAGE in (resp.headers.get('Location') or ''):
resp = self.session.get(f'{self.base_url}{_RESTARTING_PAGE}', allow_redirects=False,
timeout=timeout)
except (requests.ConnectionError, requests.Timeout):
return 'unconfirmed'
if resp.status_code not in (200, 302) or is_login_page(resp.text):
raise ModemDataInvalid(f'Restart was refused (HTTP {resp.status_code})')
if resp.status_code != 200 or is_login_page(resp.text) or 'restarting' not in resp.text.lower():
raise ModemDataInvalid(f'Restart was not started (HTTP {resp.status_code})')
return 'accepted'
def ping(self, target: str, wait_s: float = 30) -> dict:
+20 -6
View File
@@ -525,22 +525,36 @@ class TestModemRestart:
a.session.request = MagicMock(side_effect=gw)
return a, gw
def test_posts_the_restart_form(self):
REDIRECT = MagicMock(status_code=302, headers={'Location': '/cgi-bin/restarting.ha'}, text='')
RESTARTING = MagicMock(status_code=200, text='<title>Restarting</title> Your AT&T BGW320-505 is restarting.')
def test_posts_the_form_then_loads_the_restarting_page(self):
# Firmware 6.40: the post only redirects; loading restarting.ha starts the restart
a, gw = self._admin()
a.session.post = MagicMock(return_value=MagicMock(status_code=200, text='Restarting...'))
a.session.post = MagicMock(return_value=self.REDIRECT)
a.session.get = MagicMock(return_value=self.RESTARTING)
assert a.restart() == 'accepted'
data = a.session.post.call_args.kwargs['data']
assert data == {'nonce': 'ffee00112233445566778899aabbccdd', 'Restart': 'Restart'}
assert a.session.post.call_args.kwargs['data'] == {'nonce': 'ffee00112233445566778899aabbccdd',
'Restart': 'Restart'}
assert a.session.post.call_args.args[0].endswith('/cgi-bin/restart.ha')
assert a.session.get.call_args.args[0].endswith('/cgi-bin/restarting.ha')
assert ('POST', '/cgi-bin/login.ha') in gw.calls # logged in to read the form
def test_redirect_alone_is_not_a_restart(self):
a, _ = self._admin()
a.session.post = MagicMock(return_value=self.REDIRECT)
a.session.get = MagicMock(return_value=MagicMock(status_code=200, text='<title>Device</title>'))
with pytest.raises(modem.ModemDataInvalid, match='not started'):
a.restart()
def test_connection_dropped_mid_restart(self):
a, _ = self._admin()
a.session.post = MagicMock(side_effect=requests.ConnectionError('reset'))
a.session.post = MagicMock(return_value=self.REDIRECT)
a.session.get = MagicMock(side_effect=requests.ConnectionError('reset'))
assert a.restart() == 'unconfirmed'
def test_refused(self):
a, _ = self._admin()
a.session.post = MagicMock(return_value=MagicMock(status_code=200, text=LOGIN_FORM))
with pytest.raises(modem.ModemDataInvalid, match='refused'):
with pytest.raises(modem.ModemDataInvalid, match='not started'):
a.restart()