Skip to main content

Create a custom Session wrapped around the requests.Session

Project description


The csession package implements a simple wrapper class around the Session class of the requests module called CustomSession that give more freedom to the programmer to customize his session by for example setting a default timeout or headers.


pip install csession


The core of the package is the CustomSession class which mostly acts just like a normal Session

from csession import CustomSession

csess = CustomSession()

_ = csess.get("")
_ ="")

additionally, the CustomSesion accepts all the parameters for a request in the constructor to be set as default

from csession import CustomSession

csess = CustomSession(timeout=30, headers={'Content-type': 'application/json'})

# all upcoming requests will timeout after 30 seconds and have a Content-type json header


the CustomSession can also set a default prepare methode to manipulate the request before they are sent. This is for example useful for authentication purposes or url prefixing. Here is a example for a session that is prefixing every request and also adding a default password an appId to the json body of a request

from csession import CustomSession

config = {...}

# prepare function accepts methode url and params and also returns those again
# params are formatted as a dict
def prepare_microlog_request(methode, url, params):
    url = "https://my-default-url_prefix"+ url
    params["json"] = dict(params["json"], **{
        'appId': config['APP_ID'],
        'password': config['PASSWORD'],
    return methode, url, params

# Use this Session to make requests to microlog
csess = CustomSession(
    headers={'Content-type': 'application/json'},


if for a single call the prepare methode should be suppressed you can do:

from csession import CustomSession
csess = CustomSession(prepare=...)

csess.use_prepare = False
csess.use_prepare = True

or equivalently use the provided context manager:

from csession import CustomSession, without_preparation
csess = CustomSession(prepare=...)

with without_preparation(csess) as sess:

The CustomSession also provides a simple history function in for of a deque. History is deactivated by default but can be activated in the constructor by setting save_last_requests=

from csession import CustomSession, without_preparation

csess = CustomSession(save_last_requests=3) # the last 3 requests are stored



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for csession, version 0.1.4
Filename, size File type Python version Upload date Hashes
Filename, size csession-0.1.4-py3-none-any.whl (5.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size csession-0.1.4.tar.gz (4.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page