send cookie on request with urllib2

John J. Lee jjl at pobox.com
Fri Apr 21 15:21:55 EDT 2006


"itay_k" <itayyy at gmail.com> writes:

> ok.
> i will explain what exactly i wanna do.
> 
> i want to simulate the following client-side script with python:
> <body>
> <img name="Pic">
> 
> <script>
>         document.cookie="name=tom";
>         document.images["Pic"].src="temp2.html"
> </script>

Ah!  In which case what you're trying to do is a reasonable hack, but
better (UNTESTED):

import urllib2, cookielib
cj = cookielib.CookieJar
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
request = urllib2.Request(url)
response = opener.open(request)
response["Set-Cookie"] = "name=tom"
cj.extract_cookies(response, request)


If you have HTML-parsing code to extract these JS cookies that you
want to run on every request (e.g. so even cookies set by JS during
redirections get handled), you can make all this transparent very
easily by using a handler similar to HTTPCookieProcessor itself (using
a recent version of package ClientCookie here for (a recent version
of) the response_seek_wrapper class) UNTESTED:


import urllib2, cookielib

class JSHTTPCookieProcessor(urllib2.BaseHandler):
    handler_order = 400  # before HTTPCookieProcessor
    def process_response(self, request, response):
        from ClientCookie import response_seek_wrapper
        if not hasattr(response, "seek"):
            response = response_seek_wrapper(response)
        try:
            name, value = get_js_cookie(response)  # your ugly HTML parsing code here ;-)
        finally:
            response.seek(0)
        response["Set-Cookie"] = "%s=%s" % (name, value)
        return response

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(),
                              JSHTTPCookieProcessor())
response = opener.open(url)  # now we're handling JS cookies transparently!



John




More information about the Python-list mailing list