Skip to content

Commit 70100b9

Browse files
gh-79638: Restore "Treat an unreachable robots.txt as disallow all" (GH-152525)
This change was accidentally reverted by f0daba1 (gh-106693, GH-149514), which only intended to revert the ob_sval change. The tests were already restored by GH-149569. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 19b5e8e commit 70100b9

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/urllib/robotparser.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ def read(self):
6565
f = urllib.request.urlopen(self.url)
6666
except urllib.error.HTTPError as err:
6767
if err.code in (401, 403):
68+
# If access to robot.txt has the status Unauthorized/Forbidden,
69+
# then most likely this applies to the entire site.
6870
self.disallow_all = True
69-
elif err.code >= 400 and err.code < 500:
71+
elif 400 <= err.code < 500:
72+
# RFC 9309, Section 2.3.1.3: the crawler MAY access any
73+
# resources on the server.
7074
self.allow_all = True
75+
elif 500 <= err.code < 600:
76+
# RFC 9309, Section 2.3.1.4: the crawler MUST assume
77+
# complete disallow.
78+
self.disallow_all = True
7179
err.close()
7280
else:
7381
raw = f.read()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Disallow all access in :mod:`urllib.robotparser` if the ``robots.txt`` file
2+
is unreachable due to server or network errors.

0 commit comments

Comments
 (0)