Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > cookielib

Reply
Thread Tools

cookielib

 
 
Boris Ozegovic
Guest
Posts: n/a
 
      08-10-2007
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(c j))
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
 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      08-11-2007
Boris Ozegovic <(E-Mail Removed)> 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(c j))
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cookielib extracting a cookie value as a string dmbkiwi@gmail.com Python 0 01-28-2006 02:14 AM
Re: RFC 2965 cookies, cookielib, and mailman. Titus Brown Python 0 02-13-2005 08:49 PM
RFC 2965 cookies, cookielib, and mailman. John J Lee Python 0 01-30-2005 10:19 PM
cookielib and urllib2: thread-safe? Alex Hunsley Python 1 01-29-2005 12:07 PM
RFC 2965 cookies, cookielib, and mailman. C. Titus Brown Python 0 12-27-2004 08:41 PM



Advertisments