mirror of
https://github.com/python/cpython.git
synced 2024-11-25 19:03:49 +08:00
d63137159b
svn+ssh://pythondev@svn.python.org/python/trunk ........ r65209 | raymond.hettinger | 2008-07-23 19:08:18 -0500 (Wed, 23 Jul 2008) | 1 line Finish-up the partial conversion from int to Py_ssize_t for deque indices and length. ........ r65210 | raymond.hettinger | 2008-07-23 19:53:49 -0500 (Wed, 23 Jul 2008) | 1 line Parse to the correct datatype. ........ r65211 | benjamin.peterson | 2008-07-23 21:27:46 -0500 (Wed, 23 Jul 2008) | 1 line fix spacing ........ r65212 | benjamin.peterson | 2008-07-23 21:31:28 -0500 (Wed, 23 Jul 2008) | 1 line fix markup ........ r65213 | benjamin.peterson | 2008-07-23 21:45:37 -0500 (Wed, 23 Jul 2008) | 1 line add some documentation for 2to3 ........ r65214 | raymond.hettinger | 2008-07-24 00:38:48 -0500 (Thu, 24 Jul 2008) | 1 line Finish conversion from int to Py_ssize_t. ........ r65215 | raymond.hettinger | 2008-07-24 02:04:55 -0500 (Thu, 24 Jul 2008) | 1 line Convert from long to Py_ssize_t. ........ r65216 | georg.brandl | 2008-07-24 02:09:21 -0500 (Thu, 24 Jul 2008) | 2 lines Fix indentation. ........ r65225 | benjamin.peterson | 2008-07-25 11:55:37 -0500 (Fri, 25 Jul 2008) | 1 line teach .bzrignore about doc tools ........ r65226 | benjamin.peterson | 2008-07-25 12:02:11 -0500 (Fri, 25 Jul 2008) | 1 line document default value for fillvalue ........ r65233 | raymond.hettinger | 2008-07-25 13:43:33 -0500 (Fri, 25 Jul 2008) | 1 line Issue 1592: Better error reporting for operations on closed shelves. ........ r65239 | benjamin.peterson | 2008-07-25 16:59:53 -0500 (Fri, 25 Jul 2008) | 1 line fix indentation ........ r65246 | andrew.kuchling | 2008-07-26 08:08:19 -0500 (Sat, 26 Jul 2008) | 1 line This sentence continues to bug me; rewrite it for the second time ........ r65247 | andrew.kuchling | 2008-07-26 08:09:06 -0500 (Sat, 26 Jul 2008) | 1 line Remove extra words ........ r65255 | skip.montanaro | 2008-07-26 19:49:02 -0500 (Sat, 26 Jul 2008) | 3 lines Close issue 3437 - missing state change when Allow lines are processed. Adds test cases which use Allow: as well. ........ r65256 | skip.montanaro | 2008-07-26 19:50:41 -0500 (Sat, 26 Jul 2008) | 2 lines note robotparser bug fix. ........
201 lines
6.2 KiB
Python
201 lines
6.2 KiB
Python
""" robotparser.py
|
|
|
|
Copyright (C) 2000 Bastian Kleineidam
|
|
|
|
You can choose between two licenses when using this package:
|
|
1) GNU GPLv2
|
|
2) PSF license for Python 2.2
|
|
|
|
The robots.txt Exclusion Protocol is implemented as specified in
|
|
http://info.webcrawler.com/mak/projects/robots/norobots-rfc.html
|
|
"""
|
|
|
|
import urllib.parse, urllib.request
|
|
|
|
__all__ = ["RobotFileParser"]
|
|
|
|
class RobotFileParser:
|
|
""" This class provides a set of methods to read, parse and answer
|
|
questions about a single robots.txt file.
|
|
|
|
"""
|
|
|
|
def __init__(self, url=''):
|
|
self.entries = []
|
|
self.default_entry = None
|
|
self.disallow_all = False
|
|
self.allow_all = False
|
|
self.set_url(url)
|
|
self.last_checked = 0
|
|
|
|
def mtime(self):
|
|
"""Returns the time the robots.txt file was last fetched.
|
|
|
|
This is useful for long-running web spiders that need to
|
|
check for new robots.txt files periodically.
|
|
|
|
"""
|
|
return self.last_checked
|
|
|
|
def modified(self):
|
|
"""Sets the time the robots.txt file was last fetched to the
|
|
current time.
|
|
|
|
"""
|
|
import time
|
|
self.last_checked = time.time()
|
|
|
|
def set_url(self, url):
|
|
"""Sets the URL referring to a robots.txt file."""
|
|
self.url = url
|
|
self.host, self.path = urllib.parse.urlparse(url)[1:3]
|
|
|
|
def read(self):
|
|
"""Reads the robots.txt URL and feeds it to the parser."""
|
|
try:
|
|
f = urllib.request.urlopen(self.url)
|
|
except urllib.error.HTTPError as err:
|
|
if err.code in (401, 403):
|
|
self.disallow_all = True
|
|
elif err.code >= 400:
|
|
self.allow_all = True
|
|
else:
|
|
raw = f.read()
|
|
self.parse(raw.decode("utf-8").splitlines())
|
|
|
|
def _add_entry(self, entry):
|
|
if "*" in entry.useragents:
|
|
# the default entry is considered last
|
|
self.default_entry = entry
|
|
else:
|
|
self.entries.append(entry)
|
|
|
|
def parse(self, lines):
|
|
"""Parse the input lines from a robots.txt file.
|
|
|
|
We allow that a user-agent: line is not preceded by
|
|
one or more blank lines.
|
|
"""
|
|
# states:
|
|
# 0: start state
|
|
# 1: saw user-agent line
|
|
# 2: saw an allow or disallow line
|
|
state = 0
|
|
entry = Entry()
|
|
|
|
for line in lines:
|
|
if not line:
|
|
if state == 1:
|
|
entry = Entry()
|
|
state = 0
|
|
elif state == 2:
|
|
self._add_entry(entry)
|
|
entry = Entry()
|
|
state = 0
|
|
# remove optional comment and strip line
|
|
i = line.find('#')
|
|
if i >= 0:
|
|
line = line[:i]
|
|
line = line.strip()
|
|
if not line:
|
|
continue
|
|
line = line.split(':', 1)
|
|
if len(line) == 2:
|
|
line[0] = line[0].strip().lower()
|
|
line[1] = urllib.parse.unquote(line[1].strip())
|
|
if line[0] == "user-agent":
|
|
if state == 2:
|
|
self._add_entry(entry)
|
|
entry = Entry()
|
|
entry.useragents.append(line[1])
|
|
state = 1
|
|
elif line[0] == "disallow":
|
|
if state != 0:
|
|
entry.rulelines.append(RuleLine(line[1], False))
|
|
state = 2
|
|
elif line[0] == "allow":
|
|
if state != 0:
|
|
entry.rulelines.append(RuleLine(line[1], True))
|
|
state = 2
|
|
if state == 2:
|
|
self.entries.append(entry)
|
|
|
|
|
|
def can_fetch(self, useragent, url):
|
|
"""using the parsed robots.txt decide if useragent can fetch url"""
|
|
if self.disallow_all:
|
|
return False
|
|
if self.allow_all:
|
|
return True
|
|
# search for given user agent matches
|
|
# the first match counts
|
|
url = urllib.parse.quote(
|
|
urllib.parse.urlparse(urllib.parse.unquote(url))[2])
|
|
if not url:
|
|
url = "/"
|
|
for entry in self.entries:
|
|
if entry.applies_to(useragent):
|
|
return entry.allowance(url)
|
|
# try the default entry last
|
|
if self.default_entry:
|
|
return self.default_entry.allowance(url)
|
|
# agent not found ==> access granted
|
|
return True
|
|
|
|
def __str__(self):
|
|
return ''.join([str(entry) + "\n" for entry in self.entries])
|
|
|
|
|
|
class RuleLine:
|
|
"""A rule line is a single "Allow:" (allowance==True) or "Disallow:"
|
|
(allowance==False) followed by a path."""
|
|
def __init__(self, path, allowance):
|
|
if path == '' and not allowance:
|
|
# an empty value means allow all
|
|
allowance = True
|
|
self.path = urllib.parse.quote(path)
|
|
self.allowance = allowance
|
|
|
|
def applies_to(self, filename):
|
|
return self.path == "*" or filename.startswith(self.path)
|
|
|
|
def __str__(self):
|
|
return (self.allowance and "Allow" or "Disallow") + ": " + self.path
|
|
|
|
|
|
class Entry:
|
|
"""An entry has one or more user-agents and zero or more rulelines"""
|
|
def __init__(self):
|
|
self.useragents = []
|
|
self.rulelines = []
|
|
|
|
def __str__(self):
|
|
ret = []
|
|
for agent in self.useragents:
|
|
ret.extend(["User-agent: ", agent, "\n"])
|
|
for line in self.rulelines:
|
|
ret.extend([str(line), "\n"])
|
|
return ''.join(ret)
|
|
|
|
def applies_to(self, useragent):
|
|
"""check if this entry applies to the specified agent"""
|
|
# split the name token and make it lower case
|
|
useragent = useragent.split("/")[0].lower()
|
|
for agent in self.useragents:
|
|
if agent == '*':
|
|
# we have the catch-all agent
|
|
return True
|
|
agent = agent.lower()
|
|
if agent in useragent:
|
|
return True
|
|
return False
|
|
|
|
def allowance(self, filename):
|
|
"""Preconditions:
|
|
- our agent applies to this entry
|
|
- filename is URL decoded"""
|
|
for line in self.rulelines:
|
|
if line.applies_to(filename):
|
|
return line.allowance
|
|
return True
|