473,883 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create a cookie with cookielib

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.Cooki eJar()
urlOpener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cookiejar))
values = {'user':login}
data = urllib.urlencod e(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+Porte r; expires=Sat, 03-Feb-2007 11:20:05 GMT
Content-Length: 11
Connection: close
Content-Type: text/html; charset=UTF-8

Array
(
)
<cookielib.Cook ieJar[<Cookie user=Alex+Porte r 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($_COOKI E);
?>

if anyone could help... Thank you
Feb 3 '07 #1
6 5310
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

On 2/3/07, Alessandro Fachin <Al************ ***@gmail.comwr ote:
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.Cooki eJar()
urlOpener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cookiejar))
values = {'user':login}
data = urllib.urlencod e(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+Porte r; expires=Sat, 03-Feb-2007 11:20:05 GMT
Content-Length: 11
Connection: close
Content-Type: text/html; charset=UTF-8

Array
(
)
<cookielib.Cook ieJar[<Cookie user=Alex+Porte r 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($_COOKI E);
?>

if anyone could help... Thank you
--
http://mail.python.org/mailman/listinfo/python-list

--
Matthew Franz
http://www.threatmind.net/
Feb 3 '07 #2
Matthew Franz wrote:
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
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

Feb 4 '07 #3
"Matthew Franz" <md*****@gmail. comwrites:
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
Feb 4 '07 #4
Alessandro Fachin <Al************ ***@gmail.comwr ites:
Matthew Franz wrote:
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
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
Feb 4 '07 #5
John J. Lee wrote:
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.

Feb 4 '07 #6
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 <Al************ ***@gmail.comwr ites:
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.Cooki eJar()
urlOpener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cookiejar))
values = {'user':login}
data = urllib.urlencod e(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+Porte r; expires=Sat, 03-Feb-2007 11:20:05 GMT
Content-Length: 11
Connection: close
Content-Type: text/html; charset=UTF-8

Array
(
)
<cookielib.Cook ieJar[<Cookie user=Alex+Porte r 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

Feb 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5190
by: Alex Hunsley | last post by:
I'm using urllib to post data to a web form by issuing a command similar to this: filename, headers = urllib.urlretrieve("http://www.thewebsitenamehere.com/servlet/com.blah.bloo.XmlFeed", "content.txt", None, urllib.urlencode({"aParameter": "theValue"})) Now, the problem is that the above fails, since I am not sending a session cookie. Visitors to the web sites' html submission form are sent a session cookie which is given back to...
6
1712
by: Riko Wichmann | last post by:
dear all, i try to retrieve information from a secure web site. I use cookielib and urllib2 for this exercise which works to a certain level. I can authenticate myself and read the top-level page. However, from here I need to load a page which is dynamically build from information available from the page source on the top level. The desired info is inside a <form> tag which seems to be used by a javascript routine. But when I look at...
0
1408
by: dmbkiwi | last post by:
I am trying to extract the value of a cookie from a CookieJar() object using cookielib. I have a CookieJar() object called cj. Printing cj gives: <_LWPCookieJar.LWPCookieJar> But i can't work out how to extract the value. If I use make_cookies(), I get:
2
3100
by: itay_k | last post by:
Hi, I want to send a cookie on some http request (with urllib2), so I created a Cookie but I cant associate it with CookieJar object. for example: import Cookie import cookielib, urllib2
7
19093
by: itay_k | last post by:
Hi, I dont understand why this is so complicated, just to add one line of cookie header on the GET request. This is my unworking code: import time import Cookie import cookielib, urllib2
1
1867
by: BJörn Lindqvist | last post by:
Hello, I have some very serious trouble getting cookes to work. After a lot of work (urllib2 is severly underdocumented, arcane and overengineerd btw) I'm finally able to accept cookes from a server. But I'm still unable to return them to a server. Specifically the script im trying to do logs on to a server, get a session cookie and then tries to access a secure page using the same session cookie. But the cookie header cookielib...
1
6244
by: ken | last post by:
How to remove specified cookie (via a given name) in cookie jar? I have the following code, but how can I remove a specified cookie in the cookie jar? cj = cookielib.LWPCookieJar() if cj is not None: if os.path.isfile(COOKIEFILE): print 'Loading Cookie--------------' cj.load(COOKIEFILE)
2
1756
by: sophie_newbie | last post by:
Hi, I'm using the python to set a cookie when a user logs in. Thing is it doesn't seem to be setting properly in Internet Explorer. It works grand in Firefox. Its basically: c = Cookie.SimpleCookie() c = uname
5
3168
by: cbhoem | last post by:
Hi - I am trying my hand at python cookies. I'm confused about a few things though. Do the python cookies get written to a cookies text file? I have simple code below -- I see the cookie in my HTTP header but do not get anything in the cookie text file. I'm working on linux. print "Content-type: text/html" cookie = Cookie.SimpleCookie()
0
9798
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6007
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.