Eating Cookies with Python

At work, we can get to the production server logs through the browser. This seems nice, but it's a lot of clicky-clicky just to get to the log file, then you have to download it. You can look at it in a browser, but to get real examination done, you need to open it in an editor. Did I mention that there was a lot of clicky-clicky with it?

So I started to write a Python script to do this, but I ran into a snag right-away. The site uses a cookie to authorize your access to the log server. The version of Python I have installed (2.3.4 -- yep, it's old) doesn't have anything in urllib or urllib2. A few googles and a few false starts brought me to ClientCookie. Now we're talking!

Apparently, this comes with Py 2.4 as cookielib, but as I don't have 2.4, I downloaded and started working. I wasn't looking forward to putting my username/password into a script, but it turned out that I didn't have to -- ClientCookie can read your browser's cookie file and use that! This is quite cool and quite easy to do:

PYTHON:
  1. import ClientCookie
  2. cookies = ClientCookie.MozillaCookieJar()
  3. cookies.load("/path/to/firefox/directory/cookies.txt")
  4. site = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cookies))
  5. fp = site.open("http://cookie.foo.com")


fp is the same object you would get from urllib2 -- a file-like object. Just read it like a normal file!

This is so incredibly sweet and opens whole new hacking doors . . .

Leave a Reply

You must be logged in to post a comment.