Attributes

Attributes & usage examples for Sessions

Here is a simple get request. This is a wrapper around hrequests.get.

The only difference is that the session cookies are updated with each request. Creating sessions are recommended for making multiple requests to the same domain.

>>> resp = session.get('https://www.google.com/')

Session cookies update with each request:

>>> session.cookies: RequestsCookieJar
<RequestsCookieJar[Cookie(version=0, name='1P_JAR', value='2023-07-05-20', port=None, port_specified=False, domain='.google.com', domain_specified=True...

Regenerate headers for a different OS:

>>> session.os = 'win'
>>> session.headers: CaseInsensitiveDict
{'Accept': '*/*', 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0.3) Gecko/20100101 Firefox/66.0.3', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US;q=0.5,en;q=0.3', 'Cache-Control': 'max-age=0', 'DNT': '1', 'Upgrade-Insecure-Requests': '1', 'Pragma': 'no-cache'}

Closing Sessions

Sessions can also be closed to free memory:

>>> session.close()

Alternatively, sessions can be used as context managers:

with hrequests.Session() as session:
    resp = session.get('https://www.google.com/')
    print(resp)

Last updated