GithubHelp home page GithubHelp logo

Cookies without cookiejar about requests HOT 4 CLOSED

psf avatar psf commented on July 21, 2024
Cookies without cookiejar

from requests.

Comments (4)

kennethreitz avatar kennethreitz commented on July 21, 2024

I considered this originally, but TBH haven't used cookiejars enough to know what the most common/simplest method is.

Without investigating, my thought it it would be simple to roll out support for both at the same time.

Can you give me an example of how you'd pass in cookies by themselves and how that'd be useful? It would be much appreciated.

from requests.

jgorset avatar jgorset commented on July 21, 2024

I think @mcilrain might have been proposing something along the lines of...

requests.get('http://google.com', cookies={'cookie': 'value'})

I'll venture a guess that he was not aware that requests saves cookies it receives and attaches them to subsequent requests, though, which (besides being really awesome) complicates things because cookies set in this way would lack traits like expiry and domain.

It's entirely possible to create cookies programatically as it is, however it's... uh... a bit tedius:

from cookielib import CookieJar, Cookie

cookie = Cookie(
    version=0,
    name='Name',
    value='1',
    port=None,
    port_specified=False,
    domain='www.example.com',
    domain_specified=False,
    domain_initial_dot=False,
    path='/',
    path_specified=True,
    secure=False,
    expires=None,
    discard=True,
    comment=None,
    comment_url=None,
    rest={'HttpOnly': None},
    rfc2109=False
)

cookie_jar = CookieJar()
cookie_jar.set_cookie(cookie)

requests.get('http://google.com', cookies=cookie_jar)

I didn't specify 17 arguments to Cookie for fun, by the way; they're all required. It's pretty clear (and documented, too) that developers are not expected to construct their own cookies.

I can see how being able to do that might be really useful, though.

from requests.

kennethreitz avatar kennethreitz commented on July 21, 2024

Cookie.SimpleCookie() makes this easier.

import Cookie, cookielib, requests
# create cookiejar
cj = cookielib.CookieJar()
# create cookie
ck = Cookie.SimpleCookie()
ck.name = 'value'
ck.expires = 0
ck.path = '/'
ck.domain = 'www.example.com'
# add cookie to cookiejar
cj.set_cookie(ck)
# use cookiejar to make request
response = requests.get('http://www.example.com/home.php', cookies=cj)

( From http://stackoverflow.com/questions/6285378/how-can-i-use-a-cookie-to-enter-and-download-a-web-page-in-python/6285548#6285548 )

from requests.

hellman avatar hellman commented on July 21, 2024

So why not to make support both for this (dict-like)

requests.get('http://google.com', cookies={'cookie': 'value'})

and CookieJar way?

It's really useful when you need to pass cookies directly, for example PHPSESSID cookie. Yes, you can do a few more requests to login into a website and automatically set cookies, but this is too much work for that.

from requests.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.