473,604 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ClientCookie

Has anyone used ClientCookie to store cookies ?
I'm going to play around with 'pickling cookies' - but I wondered if
anyone had any experience of this.

(For session persistence in approx - my cgi proxy)

Regards,
Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #1
11 2345
fu******@gmail. com (Michael Foord) writes:
Has anyone used ClientCookie to store cookies ?
I'm going to play around with 'pickling cookies' - but I wondered if
anyone had any experience of this.

[...]

There shouldn't be any problem with that. You can pickle the
CookieJar itself, or the cookies inside it ([c for c in cookiejar] --
and use .set_cookie() to get them back into a new CookieJar).

May I suggest instead using cookielib, from Python CVS? (note that
POSTs with urllib2 are broken in 2.4a2, so don't use that)

cookielib is a new module in 2.4, and is a cleaned-up version of the
cookie-handling parts of ClientCookie.
John
Jul 18 '05 #2
fu******@gmail. com (Michael Foord) writes:
Has anyone used ClientCookie to store cookies ?
I'm going to play around with 'pickling cookies' - but I wondered if
anyone had any experience of this.

(For session persistence in approx - my cgi proxy)


Just thought to add:

1. are you using a database for this? (you should be) Look at
BSDDBCookieJar. BSDDBCookieJar isn't well-tested, but might be
just the ticket for what you're doing. It's not in cookielib yet,
but the one from ClientCookie 0.9.x should work fine with
cookielib, and I'll make it available in a separate package RSN,
along with the other stuff in ClientCookie but not in cookielib.

2. here's a simpler way (in a sense) of serialising a CookieJar than
pickling:

cj = cookielib.Cooki eJar()
....
s = [repr(c) for c in cj]
and unserialising:

cj = cookielib.Cooki eJar()
for cs in s:
cj.set_cookie(e val(cs))
....

You'd still need to write something like BSDDBCookieJar, though, if
I understand what you're doing.
John
Jul 18 '05 #3
jj*@pobox.com (John J. Lee) wrote in message news:<87******* *****@pobox.com >...
fu******@gmail. com (Michael Foord) writes:
Has anyone used ClientCookie to store cookies ?
I'm going to play around with 'pickling cookies' - but I wondered if
anyone had any experience of this. [...]

There shouldn't be any problem with that. You can pickle the
CookieJar itself, or the cookies inside it ([c for c in cookiejar] --
and use .set_cookie() to get them back into a new CookieJar).


At the moment I can't get at the cookies at *all*. Can you see what
I'm doing wrong.
Here's my code (simplified) :
import ClientCookie
openfun = ClientCookie.ur lopen

cj = ClientCookie.Co okieJar()
req = ClientCookie.Re quest(theurl, txdata, txheaders)
u = openfun(req)
info = u.info()

print '<PRE>' # This is ina CGI after all :-)
print info # This prints the headers from the server
print
print 'Cookies :'
print cj
for c in cj:
print c
print '</PRE>'
Now if I set theurl to 'http://www.google.co.u k' I get the following
response :
(txdata=None, txheaders={ 'User-agent': 'Mozilla/4.0 (compatible; MSIE
5.5; Windows NT)' })

<PRE>
Cache-Control: private
Content-Type: text/html
Set-Cookie: PREF=ID=0bac71b 03c6b1aa8:LD=en :TM=1092300998: LM=1092300998:S =YasA-Kgirv2NPnd9;
expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google. co.uk
Server: GWS/2.1
Content-Length: 2901
Date: Thu, 12 Aug 2004 08:56:38 GMT
X-Cache: MISS from dav-serv.tbsmerchan ts.co.uk
Proxy-Connection: close
Cookies :
<ClientCookie._ ClientCookie.Co okieJar[]>
</PRE>

And I'm getting this consistently - I can see a cookie in the header,
but it never appears in the CookieJar - so loading and saving the
CookieJar is of no avail.

I'm sure I'm making an obvious mistake - but short of subclassing the
CookieProcessor and doing it all manually (which seems overkill) - I'm
a bit stumped.

May I suggest instead using cookielib, from Python CVS? (note that
POSTs with urllib2 are broken in 2.4a2, so don't use that)

cookielib is a new module in 2.4, and is a cleaned-up version of the
cookie-handling parts of ClientCookie.


I'll have a look in a bit... once I can get any kind of response !!

Thanks for your help.

Fuzzy
http://www.voidspace.org.uk/atlantib...thonutils.html

John

Jul 18 '05 #4
jj*@pobox.com (John J. Lee) wrote in message news:<87******* *****@pobox.com >...
fu******@gmail. com (Michael Foord) writes:
Has anyone used ClientCookie to store cookies ?
I'm going to play around with 'pickling cookies' - but I wondered if
anyone had any experience of this.

(For session persistence in approx - my cgi proxy)
Just thought to add:

1. are you using a database for this? (you should be) Look at
BSDDBCookieJar. BSDDBCookieJar isn't well-tested, but might be
just the ticket for what you're doing. It's not in cookielib yet,
but the one from ClientCookie 0.9.x should work fine with
cookielib, and I'll make it available in a separate package RSN,
along with the other stuff in ClientCookie but not in cookielib.


The program I'm writing is a CGI. I'd like to have *minimum*
dependencies.
The version of Python on the server is 2.2 (I have no control over
that) and having a dependence on CookieClient is enough for me. I
think I can only use BSDDBCookieJar if the server has the Berkely
Database installed ? I'd like other people to be able to use my CGI on
a basic Python 2.2 install - so even if I have it on my server, I
don't want to be dependent on it.

What I'm aiming to provide is persistent cookie support for multiple
users of the same CGI - so I'll probably assign each user an id number
via a cookie I give to them and have a pickled CookieJar for each
user.

I'd also like to work towards cookie management as well - so each user
can see/edit/control which cookies they have saved.

At the moment getting it to work at all would be a bonus........
Regards,

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
2. here's a simpler way (in a sense) of serialising a CookieJar than
pickling:

cj = cookielib.Cooki eJar()
...
s = [repr(c) for c in cj]
and unserialising:

cj = cookielib.Cooki eJar()
for cs in s:
cj.set_cookie(e val(cs))
...

You'd still need to write something like BSDDBCookieJar, though, if
I understand what you're doing.
John

Jul 18 '05 #5
fu******@gmail. com (Michael Foord) wrote in message news:<6f******* *************** ****@posting.go ogle.com>...
Has anyone used ClientCookie to store cookies ?
I'm going to play around with 'pickling cookies' - but I wondered if
anyone had any experience of this.

(For session persistence in approx - my cgi proxy)

Regards,
Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html


Ok, I've got somewhere.
I'm now using the 0.9 version and I found the debugging section in the
docs.

I'm using the example from the docs - but I'm pickling a list of the
pickles rather than using the load and save methods (as the docs
suggest) because I get an NotImplemented error for the save method (so
what use is the load method ?).

COOKIEFILE = 'cookies.pkl'
import ClientCookie
openfun = ClientCookie.ur lopen

cj = ClientCookie.Co okieJar()
opener = ClientCookie.bu ild_opener(Clie ntCookie.HTTPCo okieProcessor(c j))
ClientCookie.in stall_opener(op ener)

if os.path.isfile( COOKIEFILE):
cookies = open(COOKIEFILE , 'rb')
for entry in pickle.load(coo kies):
cj.set_cookie(e ntry)
cookies.close()
cookies = open(COOKIEFILE , 'wb')
pickle.dump([c for c in cj], cookies)
cookies.close()

req = ClientCookie.Re quest(theurl)
req.add_header( 'User-agent', 'Mozilla/4.0 (compatible; MSIE 5.5;
Windows NT)')
u = openfun(req)
info = u.info()

print HR
print '<PRE>'
print info # print the headers
print
print 'Cookies :'
a = 0
for c in cj:
a += 1
print a, c.__repr__() # print each cookie
print '</PRE>'

and I'm now seeing persistent cookies..... hurrah.......
Can I save each cookie as a line of text ? Would that work with the
load method ?

Anyway - now I need to learn about server side cookies so that I can
give each user an 'id' and also write code to clean up unused cookie
files...
*Then* I need to do a seperate CGI that will let users 'manage' their
cookies. (So I'll need to start understanding cookies a bit more)

Great

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #6

fu******@gmail. com (Michael Foord) writes:
[...]
The program I'm writing is a CGI. I'd like to have *minimum*
dependencies.
The version of Python on the server is 2.2 (I have no control over
that) and having a dependence on CookieClient is enough for me. I
think I can only use BSDDBCookieJar if the server has the Berkely
Database installed ?
Yes.

I'd like other people to be able to use my CGI on
a basic Python 2.2 install - so even if I have it on my server, I
don't want to be dependent on it.

[...]

The BSDDB-wrapper is a fairly standard Python std. lib. module for
unix machines. I don't know what small-scale commercial CGI hosting
companies offer ATM, though.
John
Jul 18 '05 #7
fu******@gmail. com (Michael Foord) writes:
[...]
At the moment I can't get at the cookies at *all*. Can you see what
I'm doing wrong.
Here's my code (simplified) : [...] cj = ClientCookie.Co okieJar()
req = ClientCookie.Re quest(theurl, txdata, txheaders)
u = openfun(req)

[...]

It's not magic! You aren't using cj anywhere!
John
Jul 18 '05 #8
fu******@gmail. com (Michael Foord) writes:
[...]
Ok, I've got somewhere.
I'm now using the 0.9 version and I found the debugging section in the
docs.

I'm using the example from the docs - but I'm pickling a list of the
pickles rather than using the load and save methods (as the docs
suggest) because I get an NotImplemented error for the save method (so
what use is the load method ?).
I suppose you're trying to use FileCookieJar. That's an abstract base
class. In other words, you're not supposed to use it directly.
You're supposed to use one of its subclasses, such as LWPCookieJar,
MSIECookieJar or MozillaCookieJa r. The standard meaning of
NotImplementedE rror is not "oops, I haven't got round to that", but
rather "don't do that; use a subclass instead".

MSIECookieJar can't save cookies because the format used by Windows
isn't documented (there is an API function, but I don't use it).

[...] and I'm now seeing persistent cookies..... hurrah.......
Can I save each cookie as a line of text ? Would that work with the
load method ?
Yes. Use LWPCookieJar.

Anyway - now I need to learn about server side cookies so that I can
give each user an 'id'
Have a look at some of the Python web frameworks.

and also write code to clean up unused cookie
files...

[...]

Either

- simply always have one file per user, and persist it by whatever
means you like (LWPCookieJar, pickling,
outfile.write(' \n'.join([repr(c) for c in cookiejar])),
whatever...)

or

- write a CookieJar that hides its implementation in terms of
multiple files (and implements .clear_expired_ cookies(), etc.).
Probably not a good idea.
John
Jul 18 '05 #9
jj*@pobox.com (John J. Lee) wrote in message news:<87******* *****@pobox.com >...
fu******@gmail. com (Michael Foord) writes:
[...]
Ok, I've got somewhere.
I'm now using the 0.9 version and I found the debugging section in the
docs.

I'm using the example from the docs - but I'm pickling a list of the
pickles rather than using the load and save methods (as the docs
suggest) because I get an NotImplemented error for the save method (so
what use is the load method ?).


I suppose you're trying to use FileCookieJar. That's an abstract base
class. In other words, you're not supposed to use it directly.
You're supposed to use one of its subclasses, such as LWPCookieJar,
MSIECookieJar or MozillaCookieJa r. The standard meaning of
NotImplementedE rror is not "oops, I haven't got round to that", but
rather "don't do that; use a subclass instead".


No - I was following hte docs - which just shows CookieJar IIRC :-)
Now that I'm using LWPCookieJar everything is wonderful and works fine...

I'm nopw learning about cookies and authentication. ...

Many Thanks
Fuzzyman

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #10

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

Similar topics

28
2337
by: Mark Carter | last post by:
I am using Windows 98, python 2.3, ClientCookie 0.4.3a. When I do: import ClientCookie import os c = ClientCookie.MSIECookieJar(delayload=1) c.load_from_registry() I get the response: Traceback (most recent call last):
0
1651
by: Grant Edwards | last post by:
In the program shown below, all of the connections to the servers are using port 443 and https (TLS protocol) -- both the initial connection to login.postini.com and subsequent connections to user-3.postini.com. It works, and I don't mind that it's encrypting the sessions as well as the login, but I'd like understand why the connections to "http://user-3.postini.com/exec/MsgCtr" are being done using TLS to port 443 instead of raw TCP to...
0
1777
by: Richie Hindle | last post by:
Hi, I'm trying to write a test script to hammer an HTTP server over a persistent HTTP 1.1 connection. The server uses cookies, so I'm using a combination of ClientCookie 0.4.19 and urllib2 with Python 2.3.4 on Windows XP. This is what I have so far: import urllib2, ClientCookie UA = 'Mozilla/4.0 (compatible; MSIE 6.0;)' URL = 'http://server/resource/'
5
1783
by: Max M | last post by:
I am using ClientCookie for login on to servers and browsing them as authenticated users. I kept getting "HTTP Error 400: Bad Request" errors when submitting my forms. So I boiled it down to a simple example. When I try to use ClientCookie.urlopen() on my private network with ip numbers like "http://localhost:8081/test_site/logged_in", it works fine. If I try to call the same site through the Internet, with a url like:
4
3522
by: Neefs | last post by:
hi All, i'm using python 2.4, i get the subject error when i try to import a module (in .pyc format). Following are the details of the error: >>> import thegreek Traceback (most recent call last): File "<pyshell#22>", line 1, in -toplevel-
0
7929
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
8409
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8065
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,...
0
8280
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5882
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
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2434
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
0
1266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.