Create a cookie with cookielib

  • Thread starter Alessandro Fachin
  • Start date
A

Alessandro Fachin

Hi, i am trying to forge a new cookie by own with cookielib. But i don't
still have success. This a simply code:

import cookielib, urllib, urllib2
login = 'Ia am a cookie!'
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
values = {'user':login}
data = urllib.urlencode(values)
request = urllib2.Request("http://localhost/cookie.php", data)
url = urlOpener.open(request)
print url.info()
page = url.read(500000)
print page
print cookiejar

the output of this is:

Date: Sat, 03 Feb 2007 10:20:05 GMT
Server: Apache
X-Powered-By: PHP/5.1.6
Set-Cookie: user=Alex+Porter; expires=Sat, 03-Feb-2007 11:20:05 GMT
Content-Length: 11
Connection: close
Content-Type: text/html; charset=UTF-8

Array
(
)
<cookielib.CookieJar[<Cookie user=Alex+Porter for localhost.local/>]>

And here is the code of cookie.php that i've create for this example:

<?php
setcookie("user", "Alex Porter", time()+3600);
?>
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>

if anyone could help... Thank you
 
M

Matthew Franz

I'm not sure what you mean be forge, but if you mean set an arbitrary
cookie manually (vs. one that was provided by the server). just use
add_header() in http://docs.python.org/lib/request-objects.html

It may be possible to use CookieJar for this purpose but I've only
used it for manipulating cookies set by the server...

And I would agree that Python cookie APIs are less intuitive than
what are available in others such as Jakarta HttpClient....

- mdf

Hi, i am trying to forge a new cookie by own with cookielib. But i don't
still have success. This a simply code:

import cookielib, urllib, urllib2
login = 'Ia am a cookie!'
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
values = {'user':login}
data = urllib.urlencode(values)
request = urllib2.Request("http://localhost/cookie.php", data)
url = urlOpener.open(request)
print url.info()
page = url.read(500000)
print page
print cookiejar

the output of this is:

Date: Sat, 03 Feb 2007 10:20:05 GMT
Server: Apache
X-Powered-By: PHP/5.1.6
Set-Cookie: user=Alex+Porter; expires=Sat, 03-Feb-2007 11:20:05 GMT
Content-Length: 11
Connection: close
Content-Type: text/html; charset=UTF-8

Array
(
)
<cookielib.CookieJar[<Cookie user=Alex+Porter for localhost.local/>]>

And here is the code of cookie.php that i've create for this example:

<?php
setcookie("user", "Alex Porter", time()+3600);
?>
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>

if anyone could help... Thank you
 
A

Alessandro Fachin

Matthew said:
I'm not sure what you mean be forge, but if you mean set an arbitrary
cookie manually (vs. one that was provided by the server). just use
add_header() in http://docs.python.org/lib/request-objects.html

Yes is exactly what i want to do... i don't known because i looked at
cookielib to set cookie data, cookie are simply http header :) Inserting
values with add_header() or addheaders() it works. Thank you
 
J

John J. Lee

Matthew Franz said:
I'm not sure what you mean be forge, but if you mean set an arbitrary
cookie manually (vs. one that was provided by the server). just use
add_header() in http://docs.python.org/lib/request-objects.html

It may be possible to use CookieJar for this purpose but I've only
used it for manipulating cookies set by the server...

And I would agree that Python cookie APIs are less intuitive than
what are available in others such as Jakarta HttpClient....

There's not really intended to *be* an API, for most purposes -- you
just let it do its stuff.

What do you like from HttpClient?


John
 
J

John J. Lee

Alessandro Fachin said:
Yes is exactly what i want to do... i don't known because i looked at

No, you don't ;-)
cookielib to set cookie data, cookie are simply http header :) Inserting
values with add_header() or addheaders() it works. Thank you

Fine, but see my other post -- I think you misunderstand how cookies
work.


John
 
A

Alessandro Fachin

John said:
Fine, but see my other post -- I think you misunderstand how cookies
work.

Maybe you misunderstand me... While i wrote "cookie are simply http
header :)" i want to said that i've look at wrong thing, cookielib are not
needed... Anyway thank you for help, regards.
 
J

John J. Lee

I'm going to post this if it kills me (this was my first response in
this thread, my normal newsfeed has gone bad so can't post
reliably...)

Alessandro Fachin said:
Hi, i am trying to forge a new cookie by own with cookielib. But i don't
still have success. This a simply code:

import cookielib, urllib, urllib2
login = 'Ia am a cookie!'
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
values = {'user':login}
data = urllib.urlencode(values)
request = urllib2.Request("http://localhost/cookie.php", data)
url = urlOpener.open(request)
print url.info()
page = url.read(500000)
print page
print cookiejar

the output of this is:

Date: Sat, 03 Feb 2007 10:20:05 GMT
Server: Apache
X-Powered-By: PHP/5.1.6
Set-Cookie: user=Alex+Porter; expires=Sat, 03-Feb-2007 11:20:05 GMT
Content-Length: 11
Connection: close
Content-Type: text/html; charset=UTF-8

Array
(
)
<cookielib.CookieJar[<Cookie user=Alex+Porter for localhost.local/>]>

So the server has sent you a cookie back, and cookielib accepted it.

Success!

What your PHP program prints out is information about cookies that
were received *from* the browser (or from your script, in this case).
It does not print information about cookies that it is sending *to*
the browser. Your PHP program is not a time machine, so it can't
print out information about a cookie that was *not there* in the
request you sent. And the cookie was not there in the request you
sent because the server hadn't sent the cookie yet!

Send a second request (either in the same run of your program, or by
saving and loading the cookies), and you should see a cookie sent back
to the server (and then printed out by your PHP script in the response
you get back). Web sites and web applcations sometimes use a trick
like a using a redirect or "Refresh" to get the browser to send a
second request, so that they get the cookie they set sent back to the
server again, without the user needing to perform any second action.

Also note that saving and loading cookies with cookielib will by
default drop "session cookies", unless you explicitly ask otherwise.


John
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top