Python requests.cookies.cookiejar_from_dict() Examples

The following are 8 code examples of requests.cookies.cookiejar_from_dict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module requests.cookies , or try the search function .
Example #1
Source File: request.py    From tavern with MIT License 6 votes vote down vote up
def _set_cookies_for_request(session, request_args):
    """
    Possibly reset session cookies for a single request then set them back.
    If no cookies were present in the request arguments, do nothing.

    This does not use try/finally because if it fails then we don't care about
    the cookies anyway

    Args:
        session (requests.Session): Current session
        request_args (dict): current request arguments
    """
    if "cookies" in request_args:
        old_cookies = dict_from_cookiejar(session.cookies)
        session.cookies = cookiejar_from_dict({})
        yield
        session.cookies = cookiejar_from_dict(old_cookies)
    else:
        yield 
Example #2
Source File: gssapi_.py    From requests-gssapi with ISC License 6 votes vote down vote up
def __init__(self, response):
        super(SanitizedResponse, self).__init__()
        self.status_code = response.status_code
        self.encoding = response.encoding
        self.raw = response.raw
        self.reason = response.reason
        self.url = response.url
        self.request = response.request
        self.connection = response.connection
        self._content_consumed = True

        self._content = ""
        self.cookies = cookiejar_from_dict({})
        self.headers = CaseInsensitiveDict()
        self.headers['content-length'] = '0'
        for header in ('date', 'server'):
            if header in response.headers:
                self.headers[header] = response.headers[header] 
Example #3
Source File: client.py    From notion-py with MIT License 5 votes vote down vote up
def __init__(self, token_v2, monitor=False, start_monitoring=False, enable_caching=False, cache_key=None):
        self.session = create_session()
        self.session.cookies = cookiejar_from_dict({"token_v2": token_v2})
        if enable_caching:
            cache_key = cache_key or hashlib.sha256(token_v2.encode()).hexdigest()
            self._store = RecordStore(self, cache_key=cache_key)
        else:
            self._store = RecordStore(self)
        if monitor:
            self._monitor = Monitor(self)
            if start_monitoring:
                self.start_monitoring()
        else:
            self._monitor = None
        self._update_user_info() 
Example #4
Source File: zattoo.py    From streamlink with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _hello(self):
        log.debug('_hello ...')

        # a new session is required for the app_token
        self.session.http.cookies = cookiejar_from_dict({})
        if self.base_url == 'https://zattoo.com':
            app_token_url = 'https://zattoo.com/int/'
        else:
            app_token_url = self.base_url
        res = self.session.http.get(app_token_url)
        match = self._app_token_re.search(res.text)

        app_token = match.group(1)
        hello_url = self.API_HELLO.format(self.base_url)

        if self._uuid:
            __uuid = self._uuid
        else:
            __uuid = str(uuid.uuid4())
            self._session_attributes.set(
                'uuid', __uuid, expires=self.TIME_SESSION)

        params = {
            'client_app_token': app_token,
            'uuid': __uuid,
            'lang': 'en',
            'format': 'json'
        }
        res = self.session.http.post(hello_url, headers=self.headers, data=params) 
Example #5
Source File: http.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def _initialize(self):
        """
        .. warning: Called automatically by GoLismero. Do not call!
        """

        # Start a new session.
        self.__session = Session()

        # Load the proxy settings.
        proxy_addr = Config.audit_config.proxy_addr
        if proxy_addr:
            proxy_port = Config.audit_config.proxy_port
            if proxy_port:
                proxy_addr = "%s:%s" % (proxy_addr, proxy_port)
            auth_user = Config.audit_config.proxy_user
            auth_pass = Config.audit_config.proxy_pass
            auth, _ = detect_auth_method(proxy_addr)
            self.__session.auth = get_auth_obj(auth, auth_user, auth_pass)
            self.__session.proxies = {
                "http":  proxy_addr,
                "https": proxy_addr,
                "ftp":   proxy_addr,
            }

        # Load the cookies.
        cookie = Config.audit_config.cookie
        if cookie:
            self.__session.cookies = cookiejar_from_dict(cookie)

        # Set User Agent
        self.__user_agent = Config.audit_config.user_agent


    #-------------------------------------------------------------------------- 
Example #6
Source File: test_notify.py    From huskar with MIT License 5 votes vote down vote up
def mock_response():
    def make_response(url='', content='ok', status_code=200, history=(),
                      encoding='utf8', reason='', cookies=None):
        r = Response()
        r.url = url
        r._content = content
        r.status_code = status_code
        r.history = history
        r.encoding = encoding
        r.reason = reason
        r.cookies = cookiejar_from_dict(cookies or {})
        return r

    return make_response 
Example #7
Source File: spider.py    From persepolis with GNU General Public License v3.0 4 votes vote down vote up
def queueSpider(add_link_dictionary):
    # get download information from add_link_dictionary
    for i in ['link', 'header', 'out', 'user_agent', 'load_cookies', 'referer']:
        if not (i in add_link_dictionary):
            add_link_dictionary[i] = None

    link = add_link_dictionary['link']
    header = add_link_dictionary['header']
    user_agent = add_link_dictionary['user_agent']
    raw_cookies = add_link_dictionary['load_cookies']
    referer = add_link_dictionary['referer']

    requests_session = requests.Session()  # defining a requests Session

    if raw_cookies:  # set cookies
        cookie = SimpleCookie()
        cookie.load(raw_cookies)

        cookies = {key: morsel.value for key, morsel in cookie.items()}
        requests_session.cookies = cookiejar_from_dict(cookies)

    if referer:
        # set referer to the session
        requests_session.headers.update({'referer': referer})

    if user_agent:
        # set user_agent to the session
        requests_session.headers.update({'user-agent': user_agent})

    # find headers
    try:
        response = requests_session.head(link)
        header = response.headers
    except:
        header = {}
    filename = None
    if 'Content-Disposition' in header.keys():  # checking if filename is available
        content_disposition = header['Content-Disposition']
        if content_disposition.find('filename') != -1:
            filename_splited = content_disposition.split('filename=')
            filename_splited = filename_splited[-1]
            # getting file name in desired format
            filename = filename_splited[1:-1]

    if not(filename):
        filename = link.split('/')[-1]

    return filename 
Example #8
Source File: spider.py    From persepolis with GNU General Public License v3.0 4 votes vote down vote up
def addLinkSpider(add_link_dictionary):
    # get user's download information from add_link_dictionary
    for i in ['link', 'header', 'out', 'user_agent', 'load_cookies', 'referer']:
        if not (i in add_link_dictionary):
            add_link_dictionary[i] = None

    link = add_link_dictionary['link']
    header = add_link_dictionary['header']
    user_agent = add_link_dictionary['user_agent']
    raw_cookies = add_link_dictionary['load_cookies']
    referer = add_link_dictionary['referer']

    requests_session = requests.Session()  # defining a requests Session

    if raw_cookies:  # set cookies
        cookie = SimpleCookie()
        cookie.load(raw_cookies)

        cookies = {key: morsel.value for key, morsel in cookie.items()}
        requests_session.cookies = cookiejar_from_dict(cookies)

    if referer:
        # set referer to the session
        requests_session.headers.update({'referer': referer})

    if user_agent:
        # set user_agent to the session
        requests_session.headers.update({'user-agent': user_agent})

    # find headers
    try:
        response = requests_session.head(link)
        header = response.headers
    except:
        header = {}

    # find file size
    file_size = None
    if 'Content-Length' in header.keys():  # checking if file_size is available
        file_size = int(header['Content-Length'])

        # converting file_size to KiB or MiB or GiB
        file_size = str(humanReadableSize(file_size))

    # find file name
    file_name = None
    if 'Content-Disposition' in header.keys():  # checking if filename is available
        content_disposition = header['Content-Disposition']
        if content_disposition.find('filename') != -1:
            filename_splited = content_disposition.split('filename=')
            filename_splited = filename_splited[-1]
            # getting file name in desired format
            file_name = str(filename_splited[1:-1])

    return file_name, file_size  # If no Content-Length ? fixed it.