Debugging cookielib.CookieJar

Roy Smith roy at
Tue Jul 19 21:00:07 CEST 2011

I've got a unit test suite which instantiates an HTTP client to test
our server.  We depend on session cookies.  To handle this, I've got:

    def setUp(self):
        self.cj = cookielib.CookieJar()
        self.opener =

This works fine on one machine, and fails to return any cookies on
another.  I can watch the Set-Cookie headers go by in tcpdump and see
that there's no Cookie header in the other direction on the machine
which fails.  I can also put:

        for cookie in self.cj:
            print cookie

into one of my test methods and on the machine where it works, it
prints a cookie and on the machine where it fails, it prints nothing.

I assume there's something subtle going on with hostnames which is
causing the cookie domains to not match.  Any suggestions for
debugging this?  Is there some way to put CookieJar or
HTTPCookieProcessor into some sort of verbose mode so it logs what
it's doing?  I'm looking for the magic switch which makes it print out
"Got cookie X in domain D1, looking for cookies to return for domain
D2, X does not match because D1 != D2".

More information about the Python-list mailing list