cookielib

Discussion in 'Python' started by Boris Ozegovic, Aug 10, 2007.

  1. Hi

    I have HTTP client which accepts cookies. If client allready has cookie,
    but that cookie has expired, server sends him new cookie, and in response
    object Set-Cookie: header everything is fine, but if I reload request,
    client sends expired cookie, and not the new one. In cookiejar there is
    only new and valid cookie, and if I use regular browser everything is fine.
    The code is following:

    urlopen = urllib2.urlopen
    Request = urllib2.Request
    cj = cookielib.LWPCookieJar()
    COOKIEFILE = 'cookies.lwp'

    if os.path.isfile(COOKIEFILE):
    # if we have a cookie file already saved
    # then load the cookies into the Cookie Jar
    cj.load(COOKIEFILE)

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    url = "http://localhost:8000/files/index.html"
    params = {'question':question}
    data = urllib.urlencode(params)
    Request(url, data)
    try:
    response = urlopen(Request)
    etc.

    Only if I create new request object the new cookie is send, but I don't
    want to create new object. And idea?

    --
    Ne dajte da nas la¾ljivac Bandiæ truje:
    http://cnn.blog.hr/arhiva-2007-06.html#1622776372
     
    Boris Ozegovic, Aug 10, 2007
    #1
    1. Advertising

  2. Boris Ozegovic

    John J. Lee Guest

    Boris Ozegovic <> writes:

    > Hi
    >
    > I have HTTP client which accepts cookies. If client allready has cookie,
    > but that cookie has expired, server sends him new cookie, and in response
    > object Set-Cookie: header everything is fine, but if I reload request,
    > client sends expired cookie, and not the new one. In cookiejar there is
    > only new and valid cookie, and if I use regular browser everything is fine.
    > The code is following:
    >
    > urlopen = urllib2.urlopen
    > Request = urllib2.Request
    > cj = cookielib.LWPCookieJar()
    > COOKIEFILE = 'cookies.lwp'
    >
    > if os.path.isfile(COOKIEFILE):
    > # if we have a cookie file already saved
    > # then load the cookies into the Cookie Jar
    > cj.load(COOKIEFILE)
    >
    > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    > urllib2.install_opener(opener)
    > url = "http://localhost:8000/files/index.html"
    > params = {'question':question}
    > data = urllib.urlencode(params)
    > Request(url, data)
    > try:
    > response = urlopen(Request)
    > etc.
    >
    > Only if I create new request object the new cookie is send, but I don't
    > want to create new object. And idea?


    You seem to suggest in your first paragraph that you call urlopen more
    than once, but in the code you show, you call it only once.

    This is the way HTTP cookies work: you can't send back a cookie before
    you've received it. So, you need two HTTP requests: one to get the
    cookie, and a second to send it back again.

    However, some websites arrange things so that a single attempt to load
    a URL results in multiple HTTP requests. One common way to do that is
    by using an "HTTP Refresh" -- look at the top of the HTML to see if
    there's a META tag something like this:

    <meta http-equiv="refresh" content="1" />

    This means that the browser should reload the page after a pause of
    one second. Often, the reloaded page then does not contain another
    meta refresh tag, so that the page does not reload every second
    forever (the server knows not to include the tag because it received
    the cookie).

    This tag can be supported by appropriate Python code. So, two HTTP
    requests needn't necessarily mean two urlopen calls. This library
    handles refresh redirections, and supports essentially the same
    interface as urllib2 and cookielib (the union of the two interfaces):

    http://wwwsearch.sourceforge.net/mechanize/


    John
     
    John J. Lee, Aug 11, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C. Titus Brown

    RFC 2965 cookies, cookielib, and mailman.

    C. Titus Brown, Dec 27, 2004, in forum: Python
    Replies:
    0
    Views:
    504
    C. Titus Brown
    Dec 27, 2004
  2. Alex Hunsley

    cookielib and urllib2: thread-safe?

    Alex Hunsley, Jan 26, 2005, in forum: Python
    Replies:
    1
    Views:
    478
    John J. Lee
    Jan 29, 2005
  3. John J Lee
    Replies:
    0
    Views:
    354
    John J Lee
    Jan 30, 2005
  4. Titus Brown
    Replies:
    0
    Views:
    477
    Titus Brown
    Feb 13, 2005
  5. Replies:
    0
    Views:
    576
Loading...

Share This Page