added default headers for get_default_session
This commit is contained in:
1 parent
bb24d0c13a
commit
d307be60b0
1 file changed
+12
-2
@@ -15,16 +15,26 @@ from aioweb_tls import CurlCffi, TLSSession
|
|||||||
from monitor_utils.constants import CLIENT_IDENTIFIERS, RETRY_LIMIT
|
from monitor_utils.constants import CLIENT_IDENTIFIERS, RETRY_LIMIT
|
||||||
|
|
||||||
|
|
||||||
def get_default_session(proxies: AioProxies) -> TLSSession:
|
def get_default_session(
|
||||||
|
proxies: AioProxies, headers: dict | None = None
|
||||||
|
) -> TLSSession:
|
||||||
"""Return a TLSSession impersonating a random client fingerprint.
|
"""Return a TLSSession impersonating a random client fingerprint.
|
||||||
|
|
||||||
Fingerprint rotation lives here so every monitor rotates the same way; the
|
Fingerprint rotation lives here so every monitor rotates the same way; the
|
||||||
caller owns the session lifetime via ``async with``.
|
caller owns the session lifetime via ``async with``.
|
||||||
|
|
||||||
|
Cookies are not set here: aioweb has no constructor cookie path, and
|
||||||
|
monitors usually obtain them at runtime, so call session.set_cookie(name,
|
||||||
|
value, domain=...) where they are fetched (e.g. a pre_task_func) instead.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
proxies: Pool attached to the session as proxy_pool, which send_request
|
proxies: Pool attached to the session as proxy_pool, which send_request
|
||||||
rotates through per request. Required so a session can never send
|
rotates through per request. Required so a session can never send
|
||||||
un-proxied.
|
un-proxied.
|
||||||
|
headers: Default headers merged into every request by aioweb. Use for
|
||||||
|
monitor-wide static headers; per-request headers still go through
|
||||||
|
send_request. The curl_cffi impersonation already supplies a
|
||||||
|
matching User-Agent, so one is not defaulted here.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A TLSSession over a curl_cffi backend with a random impersonation, with
|
A TLSSession over a curl_cffi backend with a random impersonation, with
|
||||||
@@ -32,7 +42,7 @@ def get_default_session(proxies: AioProxies) -> TLSSession:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
impersonate = random.choice(CLIENT_IDENTIFIERS)
|
impersonate = random.choice(CLIENT_IDENTIFIERS)
|
||||||
session = TLSSession(backend=CurlCffi(impersonate=impersonate))
|
session = TLSSession(backend=CurlCffi(impersonate=impersonate), headers=headers)
|
||||||
session.proxy_pool = proxies
|
session.proxy_pool = proxies
|
||||||
return session
|
return session
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user