MCPcopy Create free account
hub / github.com/apify/crawlee-python / Session

Class Session

src/crawlee/sessions/_session.py:24–239  ·  view source on GitHub ↗

Represent a single user session, managing cookies, error states, and usage limits. A `Session` simulates a specific user with attributes like cookies, IP (via proxy), and potentially a unique browser fingerprint. It maintains its internal state, which can include custom user data (e.g.,

Source from the content-addressed store, hash-verified

22
23@docs_group('Session management')
24class Session:
25 """Represent a single user session, managing cookies, error states, and usage limits.
26
27 A `Session` simulates a specific user with attributes like cookies, IP (via proxy), and potentially
28 a unique browser fingerprint. It maintains its internal state, which can include custom user data
29 (e.g., authorization tokens or headers) and tracks its usability through metrics such as error score,
30 usage count, and expiration.
31 """
32
33 _DEFAULT_BLOCKED_STATUS_CODES: ClassVar = [401, 403, 429]
34 """Default status codes that indicate a session is blocked."""
35
36 def __init__(
37 self,
38 *,
39 id: str | None = None,
40 max_age: timedelta = timedelta(minutes=50),
41 user_data: Mapping[str, JsonSerializable] | None = None,
42 max_error_score: float = 3.0,
43 error_score_decrement: float = 0.5,
44 created_at: datetime | None = None,
45 usage_count: int = 0,
46 max_usage_count: int = 50,
47 error_score: float = 0.0,
48 cookies: SessionCookies | CookieJar | dict[str, str] | list[CookieParam] | None = None,
49 blocked_status_codes: list | None = None,
50 ) -> None:
51 """Initialize a new instance.
52
53 Args:
54 id: Unique identifier for the session, autogenerated if not provided.
55 max_age: Time duration after which the session expires.
56 user_data: Custom user data associated with the session.
57 max_error_score: Threshold score beyond which the session is considered blocked.
58 error_score_decrement: Value by which the error score is decremented on successful operations.
59 created_at: Timestamp when the session was created, defaults to current UTC time if not provided.
60 usage_count: Number of times the session has been used.
61 max_usage_count: Maximum allowable uses of the session before it is considered expired.
62 error_score: Current error score of the session.
63 cookies: Cookies associated with the session.
64 blocked_status_codes: HTTP status codes that indicate a session should be blocked.
65 """
66 self._id = id or crypto_random_object_id(length=10)
67 self._max_age = max_age
68 self._user_data: MutableMapping[str, JsonSerializable] = dict(user_data) if user_data is not None else {}
69 self._max_error_score = max_error_score
70 self._error_score_decrement = error_score_decrement
71 self._created_at = created_at or datetime.now(timezone.utc)
72 self._usage_count = usage_count
73 self._max_usage_count = max_usage_count
74 self._error_score = error_score
75 self._cookies = SessionCookies(cookies) or SessionCookies()
76 self._blocked_status_codes = set(blocked_status_codes or self._DEFAULT_BLOCKED_STATUS_CODES)
77
78 @classmethod
79 def from_model(cls, model: SessionModel) -> Session:
80 """Initialize a new instance from a `SessionModel`."""
81 cookies = SessionCookies(model.cookies)

Calls

no outgoing calls