How to use the mechanize.CookieJar function in mechanize

To help you get started, we’ve selected a few mechanize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_empty_path(self):
        from mechanize import CookieJar, Request, DefaultCookiePolicy

        # Test for empty path
        # Broken web-server ORION/1.3.38 returns to the client response like
        #
        #  Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=
        #
        # ie. with Path set to nothing.
        # In this case, extract_cookies() must set cookie to / (root)
        c = CookieJar(DefaultCookiePolicy(rfc2965=True))
        headers = []

        req = Request("http://www.ants.com/")
        headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")
        res = FakeResponse(headers, "http://www.ants.com/")
        c.extract_cookies(res, req)

        req = Request("http://www.ants.com/")
        c.add_cookie_header(req)

        assert (req.get_header("Cookie") == "JSESSIONID=ABCDERANDOM123"
                and req.get_header("Cookie2") == '$Version="1"')

        # missing path in the request URI
        req = Request("http://www.ants.com:8080")
        c.add_cookie_header(req)
github python-mechanize / mechanize / test / test_cookies.py View on Github external
# Netscape

        c = CookieJar()
        interact_netscape(c, "http://www.acme.com/", 'spam="bar"')
        assert "/" in c._cookies["www.acme.com"]

        c = CookieJar()
        interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')
        assert "/" in c._cookies["www.acme.com"]

        c = CookieJar()
        interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')
        assert "/blah" in c._cookies["www.acme.com"]

        c = CookieJar()
        interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
        assert "/blah/rhubarb" in c._cookies["www.acme.com"]
github python-mechanize / mechanize / test / test_functional.py View on Github external
def test_cookies(self):
        # this test page depends on cookies, and an http-equiv refresh
        # cj = CreateBSDDBCookieJar("/home/john/db.db")
        cj = CookieJar()
        handlers = [
            HTTPCookieProcessor(cj),
            HTTPRefreshProcessor(max_time=None, honor_time=False),
            HTTPEquivProcessor(),

            HTTPRedirectHandler(),  # needed for Refresh handling in 2.4.0
            #            HTTPHandler(True),
            #            HTTPRedirectDebugProcessor(),
            #            HTTPResponseDebugProcessor(),
        ]

        opener = self.build_opener(handlers)
        r = opener.open(urljoin(self.uri, "/dynamic"))
        data = r.read()
        self.assertIn(b"Your browser supports cookies!", data)
        self.assertEqual(len(cj), 2)
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_domain_mirror(self):
        from mechanize import CookieJar, DefaultCookiePolicy

        pol = DefaultCookiePolicy(rfc2965=True)

        c = CookieJar(pol)
        url = "http://foo.bar.com/"
        interact_2965(c, url, "spam=eggs; Version=1")
        h = interact_2965(c, url)
        assert h.find("Domain") == -1, \
            "absent domain returned with domain present"

        c = CookieJar(pol)
        url = "http://foo.bar.com/"
        interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
        h = interact_2965(c, url)
        assert h.find('$Domain=".bar.com"') != -1, \
            "domain not returned"

        c = CookieJar(pol)
        url = "http://foo.bar.com/"
        # note missing initial dot in Domain
github norbusan / calibre-debian / src / calibre / ebooks / metadata / sources / overdrive.py View on Github external
clean_cj = mechanize.CookieJar()
        br.set_cookiejar(clean_cj)
        # get the base url to set the proper session cookie
        br.open_novisit(q)

        # initialize the search
        self.safe_query(br, search_url)

        # get the results
        req = mechanize.Request(results_url)
        req.add_header('X-Requested-With', 'XMLHttpRequest')
        req.add_header('Referer', search_url)
        req.add_header('Accept', 'application/json, text/javascript, */*')
        raw = br.open_novisit(req)
        raw = type('')(list(raw))
        clean_cj = mechanize.CookieJar()
        br.set_cookiejar(clean_cj)
        return self.sort_ovrdrv_results(raw, log, None, None, None, ovrdrv_id)
github kovidgoyal / calibre / src / calibre / ebooks / metadata / sources / overdrive.py View on Github external
def overdrive_get_record(self, br, log, q, ovrdrv_id):
        import mechanize
        search_url = q+'SearchResults.aspx?ReserveID={'+ovrdrv_id+'}'
        results_url = q+'SearchResults.svc/GetResults?sEcho=1&iColumns=18&sColumns=ReserveID%2CTitle%2CSubtitle%2CEdition%2CSeries%2CPublisher%2CFormat%2CFormatID%2CCreators%2CThumbImage%2CShortDescription%2CWorldCatLink%2CExcerptLink%2CCreatorFile%2CSortTitle%2CAvailableToLibrary%2CAvailableToRetailer%2CRelevancyRank&iDisplayStart=0&iDisplayLength=10&sSearch=&bEscapeRegex=true&iSortingCols=1&iSortCol_0=17&sSortDir_0=asc'  # noqa

        # re-initialize the cookiejar to so that it's clean
        clean_cj = mechanize.CookieJar()
        br.set_cookiejar(clean_cj)
        # get the base url to set the proper session cookie
        br.open_novisit(q)

        # initialize the search
        self.safe_query(br, search_url)

        # get the results
        req = mechanize.Request(results_url)
        req.add_header('X-Requested-With', 'XMLHttpRequest')
        req.add_header('Referer', search_url)
        req.add_header('Accept', 'application/json, text/javascript, */*')
        raw = br.open_novisit(req)
        raw = type('')(list(raw))
        clean_cj = mechanize.CookieJar()
        br.set_cookiejar(clean_cj)
github laurentb / weboob / weboob / deprecated / browser / firefox_cookies.py View on Github external
def __init__(self, domain, sqlite_file=None, policy=None):
        CookieJar.__init__(self, policy)

        self.domain = domain
        self.sqlite_file = sqlite_file
github python-mechanize / mechanize / attic / BSDDBCookieJar.py View on Github external
item = self._c.next()
                if item is None:
                    self.close()
                    raise StopIteration()
                domain, data = item
                self._i = MappingIterator(pickle.loads(data))
            try:
                return self._i.next()
            except StopIteration:
                self._i = None
                continue
    def __del__(self):
        # XXXX will this work?
        self.close()

class BSDDBCookieJar(CookieJar):
    """CookieJar based on a BSDDB database, using the standard bsddb module.

    You should use CreateBSDDBCookieJar instead of the constructor, unless you
    know what you're doing.

    Note that session cookies ARE stored in the database (marked as session
    cookies), and will be written to disk if the database is file-based.  In
    order to clear session cookies at the end of a session, you must call
    .clear_session_cookies().

    Call the .close() method after you've finished using an instance of this
    class.

    **********************************************************************
    THIS IS NOT FULLY TESTED!
    **********************************************************************