Collection of HTTP cookies. You may not need to know about this class: try urllib.request.build_opener(HTTPCookieProcessor).open(url).
| 1245 | class Absent: pass |
| 1246 | |
| 1247 | class CookieJar: |
| 1248 | """Collection of HTTP cookies. |
| 1249 | |
| 1250 | You may not need to know about this class: try |
| 1251 | urllib.request.build_opener(HTTPCookieProcessor).open(url). |
| 1252 | """ |
| 1253 | |
| 1254 | non_word_re = re.compile(r"\W") |
| 1255 | quote_re = re.compile(r"([\"\\])") |
| 1256 | strict_domain_re = re.compile(r"\.?[^.]*") |
| 1257 | domain_re = re.compile(r"[^.]*") |
| 1258 | dots_re = re.compile(r"^\.+") |
| 1259 | |
| 1260 | magic_re = re.compile(r"^\#LWP-Cookies-(\d+\.\d+)", re.ASCII) |
| 1261 | |
| 1262 | def __init__(self, policy=None): |
| 1263 | if policy is None: |
| 1264 | policy = DefaultCookiePolicy() |
| 1265 | self._policy = policy |
| 1266 | |
| 1267 | self._cookies_lock = _threading.RLock() |
| 1268 | self._cookies = {} |
| 1269 | |
| 1270 | def set_policy(self, policy): |
| 1271 | self._policy = policy |
| 1272 | |
| 1273 | def _cookies_for_domain(self, domain, request): |
| 1274 | cookies = [] |
| 1275 | if not self._policy.domain_return_ok(domain, request): |
| 1276 | return [] |
| 1277 | _debug("Checking %s for cookies to return", domain) |
| 1278 | cookies_by_path = self._cookies[domain] |
| 1279 | for path in cookies_by_path.keys(): |
| 1280 | if not self._policy.path_return_ok(path, request): |
| 1281 | continue |
| 1282 | cookies_by_name = cookies_by_path[path] |
| 1283 | for cookie in cookies_by_name.values(): |
| 1284 | if not self._policy.return_ok(cookie, request): |
| 1285 | _debug(" not returning cookie") |
| 1286 | continue |
| 1287 | _debug(" it's a match") |
| 1288 | cookies.append(cookie) |
| 1289 | return cookies |
| 1290 | |
| 1291 | def _cookies_for_request(self, request): |
| 1292 | """Return a list of cookies to be returned to server.""" |
| 1293 | cookies = [] |
| 1294 | for domain in self._cookies.keys(): |
| 1295 | cookies.extend(self._cookies_for_domain(domain, request)) |
| 1296 | return cookies |
| 1297 | |
| 1298 | def _cookie_attrs(self, cookies): |
| 1299 | """Return a list of cookie-attributes to be returned to server. |
| 1300 | |
| 1301 | like ['foo="bar"; $Path="/"', ...] |
| 1302 | |
| 1303 | The $Version attribute is also added when appropriate (currently only |
| 1304 | once per request). |
searching dependent graphs…