[Tutor] Cookies and authorization

D. Hartley denise.hartley at gmail.com
Thu Jun 23 19:57:27 CEST 2005


Hello, everyone!

I am trying to go to a website, collect any and all cookies I receive
by going to that website, and then look at the cookies/print them.

So I did the following, from the cookie examples in the documentation:

import cookielib, urllib2
myjar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(myjar))
x = opener.open("http://www.amazon.com")

(I just did amazon because I knew it would leave cookies, as a sample). 

I'm not entirely sure how this works yet, because I'm just learning
about cookies now, but I am going to play with it and see what I can
find out.  My question for you is this:

The website I have to access and get the cookies from is, of course,
not amazon, it is:

http://www.pythonchallenge.com/pc/return/__________.html

..where ____________ is an actual word, but I'm not typing it so I
don't give away any spoilers to people who are working on the
challenges.

My problem is, when I plug this url into my sample code above, I get
an error ("HTTP Error 401: Authorization Required"), because normally
when you go to this url it makes you enter in a username and a
password.  Does anyone know how to get around this, either with code
commands I can change to embed the password/username, or a way I can
reformat the URL to *include* the password/username?  I remember
seeing something like this but I can't get it formatted right.  Say my
username is "guido" and my password is "python."

Any help would be much appreciated! Thanks :)

~Denise

(Also, there is lots of documentation on the cookie modules, and, to
my own great amusement, even examples(!!), but if anyone has a simple
howto for cookie processing, that would also be a great resource!)


More information about the Tutor mailing list