Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ClientCookie bug

Reply
Thread Tools

ClientCookie bug

 
 
Mark Carter
Guest
Posts: n/a
 
      08-14-2003
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):
File "C:\My Documents\mycookies.py", line 33, in ?
c.load_from_registry()
File "C:\PYTHON23\Lib\site-packages\ClientCookie\_MSIECookieJar.py",
line 230, in load_from_registry
self.load(filename, ignore_discard, ignore_expires)
File "C:\PYTHON23\Lib\site-packages\ClientCookie\_MSIECookieJar.py",
line 245, in load
self._really_load(index, filename, ignore_discard, ignore_expires)
File "C:\PYTHON23\Lib\site-packages\ClientCookie\_MSIECookieJar.py",
line 258, in _really_load
user_name = string.lower(os.environ['USERNAME'])
File "C:\PYTHON23\Lib\os.py", line 417, in __getitem__
return self.data[key.upper()]
KeyError: 'USERNAME'

Basically, it doesn't like USERNAME as an environment variable.
 
Reply With Quote
 
 
 
 
Syver Enstad
Guest
Posts: n/a
 
      08-14-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Mark Carter) writes:

> 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):
> File "C:\My Documents\mycookies.py", line 33, in ?
> c.load_from_registry()
> File "C:\PYTHON23\Lib\site-packages\ClientCookie\_MSIECookieJar.py",
> line 230, in load_from_registry
> self.load(filename, ignore_discard, ignore_expires)
> File "C:\PYTHON23\Lib\site-packages\ClientCookie\_MSIECookieJar.py",
> line 245, in load
> self._really_load(index, filename, ignore_discard, ignore_expires)
> File "C:\PYTHON23\Lib\site-packages\ClientCookie\_MSIECookieJar.py",
> line 258, in _really_load
> user_name = string.lower(os.environ['USERNAME'])
> File "C:\PYTHON23\Lib\os.py", line 417, in __getitem__
> return self.data[key.upper()]
> KeyError: 'USERNAME'
>
> Basically, it doesn't like USERNAME as an environment variable.


I suspect this only works on "real" win32 (the NT family) operating
systems, which define USERNAME, and not hybrid dos/win32 systems like
the win9x family.


 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      08-14-2003
Syver Enstad <(E-Mail Removed)> writes:

> (E-Mail Removed) (Mark Carter) writes:
>
> > 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:

[...]
> > KeyError: 'USERNAME'
> >
> > Basically, it doesn't like USERNAME as an environment variable.

>
> I suspect this only works on "real" win32 (the NT family) operating
> systems, which define USERNAME, and not hybrid dos/win32 systems like
> the win9x family.


Anybody know the best way to get the username without win32all
installed?

Mark: I don't have a win9x box, but try replacing that
os.environ['USERNAME'] with getpass.getuser(). You'll need to stick
an import getpass in there too, of course. Let me know if that works.

The rest of the code should work OK on win9x.


John
 
Reply With Quote
 
Mark Carter
Guest
Posts: n/a
 
      08-14-2003
> > I am using Windows 98, python 2.3, ClientCookie 0.4.3a.
> >
> > When I do:
> > c.load_from_registry()
> >
> > I get the response:
> > KeyError: 'USERNAME'


> I suspect this only works on "real" win32 (the NT family) operating


I have a fix/workaround for this - and would like it included in ClientCookie.
It my problems out real nice! Interested?

BTW, I can't seem to locate the project on Sourceforge.
 
Reply With Quote
 
Mark Carter
Guest
Posts: n/a
 
      08-14-2003
(E-Mail Removed) (John J. Lee) wrote in message news:<(E-Mail Removed)>...
> Syver Enstad <(E-Mail Removed)> writes:
>
> > (E-Mail Removed) (Mark Carter) writes:
> >
> > > 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:

> [...]
> > > KeyError: 'USERNAME'
> > >
> > > Basically, it doesn't like USERNAME as an environment variable.

> >
> > I suspect this only works on "real" win32 (the NT family) operating
> > systems, which define USERNAME, and not hybrid dos/win32 systems like
> > the win9x family.

>
> Anybody know the best way to get the username without win32all
> installed?
>
> Mark: I don't have a win9x box, but try replacing that
> os.environ['USERNAME'] with getpass.getuser(). You'll need to stick
> an import getpass in there too, of course. Let me know if that works.
>
> The rest of the code should work OK on win9x.
>
>
> John


My apologies - the bug exists in my own brain, not in the code. I had in mind
that a fix would be required in ClientCookie - but now I know better.

To get it to work, I simply called:
os.environ['USERNAME'] = 'mark carter' # or whatever your name is
before calling
c.load_from_registry() # c is a MSIECookieJar

It's an embarassingly simple solution to the problem - but it took me a while
to figure it out. Although there is nothing wrong with code, perhaps it would
help new users of the library to mention it. Maybe the solution is somewhat
obvious in retrospect - but it took me a while before the penny dropped.

Another approach - which I actually prefer - is to copy the cookie to the local
directory and load it using load_cookie_data(). At first, I was having no end
of problems with it - until I discovered that you should use the (binary)
cookie, not an ASCII cookie that you obtain from performing a cookie export
from MSIE. ... and don't forget to call ClientCookie.MSIECookieJar() WITHOUT
the delayload argument.

So, sorry for the false alarm - but hope my investigations will prove useful to
others.
 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      08-15-2003
(E-Mail Removed) (Mark Carter) writes:

> (E-Mail Removed) (John J. Lee) wrote in message news:<(E-Mail Removed)>...

[...]
> My apologies - the bug exists in my own brain, not in the code. I had in mind


Your brain is fine, Mark. It's a real bug.


> that a fix would be required in ClientCookie - but now I know better.
>
> To get it to work, I simply called:
> os.environ['USERNAME'] = 'mark carter' # or whatever your name is
> before calling
> c.load_from_registry() # c is a MSIECookieJar


That's a workaround, but it shouldn't be necessary. Would you mind
trying the getpass fix I suggested? I don't know if getpass.getuser
works on w9x, but it's worth a shot.


[...]
> Another approach - which I actually prefer - is to copy the cookie to the local
> directory and load it using load_cookie_data(). At first, I was having no end


Should be no need for that.


> of problems with it - until I discovered that you should use the (binary)
> cookie, not an ASCII cookie that you obtain from performing a cookie export
> from MSIE. ... and don't forget to call ClientCookie.MSIECookieJar() WITHOUT
> the delayload argument.

[...]

Why without the delayload argument? There are features that don't yet
work with that, but that's documented. Is something else not working?


John
 
Reply With Quote
 
JanC
Guest
Posts: n/a
 
      08-16-2003
(E-Mail Removed) (Mark Carter) schreef:

> BTW, I can't seem to locate the project on Sourceforge.


<http://wwwsearch.sourceforge.net/>

--
JanC

"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9
 
Reply With Quote
 
Mark Carter
Guest
Posts: n/a
 
      08-16-2003
> I'll try it on a win 98 machine, and report back the results asap.

---
USERNAME

I promised that I would try getuser() on a win 98 machine. Alas,
typing:

import getpass
print getpass.getuser()

produces:

ImportError: No module named pwd

So, it's the same problem as occurs on win95 (unsuprisingly)

---
DELAYLOAD

John: Why without the delayload argument?

My response:

The following code:

import ClientCookie
c = ClientCookie.MSIECookieJar(delayload=1)
c.load_cookie_data("hemscott-cookie.bin")
import urllib2
url = 'http://businessplus.hemscott.net/corp/crp03733.htm'
request = urllib2.Request(url)
response = urllib2.urlopen(request)
request2 = urllib2.Request(url)
c.add_cookie_header(request2)
response2 = urllib2.urlopen(request2)
print response2.geturl()
print response2.info() # headers
for line in response2.readlines(): # body
print line


produces the error:

Traceback (most recent call last):
File "C:\My Documents\markc\ClientCookie\clp04.py", line 9, in ?
c.add_cookie_header(request2)
File "C:\PYTHON23\Lib\site-packages\ClientCookie\_ClientCookie.py",
line 1170, in add_cookie_header
cookies.extend(self._get_cookies_for_domain(
File "C:\PYTHON23\Lib\site-packages\ClientCookie\_ClientCookie.py",
line 1050, in _get_cookies_for_domain
return self._cookies_for_domain(domain, request, unverifiable)
File "C:\PYTHON23\lib\site-packages\ClientCookie\_MSIECookieJar.py",
line 112, in _cookies_for_domain
if self.delayload and cookies["//+delayload"] is not None:
KeyError: '//+delayload'

whereas running it without delayload=1 causes it to run successfully.
 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      08-16-2003
(E-Mail Removed) (Mark Carter) writes:
[...]
> John: Why without the delayload argument?
>
> My response:
>
> The following code:
>
> import ClientCookie
> c = ClientCookie.MSIECookieJar(delayload=1)
> c.load_cookie_data("hemscott-cookie.bin")
> import urllib2
> url = 'http://businessplus.hemscott.net/corp/crp03733.htm'
> request = urllib2.Request(url)
> response = urllib2.urlopen(request)
> request2 = urllib2.Request(url)
> c.add_cookie_header(request2)
> response2 = urllib2.urlopen(request2)
> print response2.geturl()
> print response2.info() # headers
> for line in response2.readlines(): # body
> print line


Take note of this comment from the web page:

| # Don't copy this blindly! You probably want to follow the examples
| # above, not this one.

No matter how many times and how many places I say this, everybody
seems to be driven to make the same mistakes. I guess people don't
believe it's as simple as calling urlopen, though I explictly say it
*is* that simple in most cases in the second sentence of the
documentation.

DON'T USE *BOTH* add_cookie_header/extract_cookies *AND* urlopen.
urlopen is all you need, unless you're not using urllib2. Probably
won't do any actual harm, but it's completely pointless and
obfuscatory. You're also failing to use ClientCookie.urlopen (which
unlike urllib2.urlopen, knows about cookies), because you blindly
copied the third example from the web page. You're also using Request
objects for no apparent reason, and you're attempting to fetch the
same url twice, again because you're blindly copying. Sigh -- if
people have to blindly copy, why not copy from the code I actually
*tell* people to copy from, instead of the code I explictly tell
people *not* to copy from? Ngghhh!

You want something like this:

import ClientCookie
c = ClientCookie.MSIECookieJar(delayload=1)
c.load_cookie_data("hemscott-cookie.bin")
url = 'http://businessplus.hemscott.net/corp/crp03733.htm'
response = ClientCookie.urlopen(url)

print response.read()
response.close()


> produces the error:
>
> Traceback (most recent call last):

[...]
> whereas running it without delayload=1 causes it to run successfully.


Anyway, you have found another bug, so all is forgiven.

(the MSIE delayload feature is still a bit of a mess -- luckily, I can
pin the blame on the original author of that code, since it's pretty
directly ported from Perl


John
 
Reply With Quote
 
Mark Carter
Guest
Posts: n/a
 
      08-17-2003
> *tell* people to copy from, instead of the code I explictly tell
> people *not* to copy from? Ngghhh!



> Anyway, you have found another bug, so all is forgiven.


> (the MSIE delayload feature is still a bit of a mess -- luckily, I can
> pin the blame on the original author of that code, since it's pretty
> directly ported from Perl



If we shadows have offended,
Think but this, and all is mended-
That you have but slumb'red here
While these visions did appear.
And this weak and idle theme,
No more yielding but a dream,
Gentles, do not reprehend.
If you pardon, we will mend.
And, as I am an honest Puck,
If we have unearned luck
Now to scape the serpent's tongue,
We will make amends ere long;
Else the Puck a liar call.
So, good night unto you all.
Give me your hands, if we be friends,
And Robin shall restore amends.


But seriously ... please standby and I will inform you of the outcome
of your changes.
 
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
ClientCookie problem - Difference between 'post' on the local networkand the internet Max M Python 5 10-26-2004 09:36 PM
urllib2 / ClientCookie / Keep-Alive Richie Hindle Python 0 10-15-2004 02:18 PM
ClientCookie Michael Foord Python 11 08-23-2004 02:40 PM
ClientCookie/urllib2 with persistent connections? Chuck Bearden Python 2 05-11-2004 03:55 AM
Why is ClientCookie/urllib2 using https? Grant Edwards Python 0 09-24-2003 08:06 PM



Advertisments