Create a cookie with cookielib

Alessandro Fachin Alessandro.Fachin at gmail.com
Sat Feb 3 05:23:22 EST 2007


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



More information about the Python-list mailing list