hi Fran,

I posted an example for using cookies within standard Python and GAE
here:
http://groups.google.com/group/web2py/browse_thread/thread/519fca71f781aef5


The relevant part is:
__________________________________________________________________

import urllib
import urllib2
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor()))

def download(url, data=None, user_agent='Mozilla/5.0'):
    data = data if data is None else urllib.urlencode(data)
    headers = {'User-agent': user_agent}
    request = urllib2.Request(url, data, headers)
    return urllib2.urlopen(request).read()
__________________________________________________________________

I agree it would be good to make fetch() more useful by supporting
sessions.

Richard


On Feb 11, 7:00 am, Fran <francisb...@googlemail.com> wrote:
> I want to add support for sessions to fetch() from tools.py.
> This is so that I can call protected KML feeds from inside the Sahana
> Map Viewing Client.
> I think that what I need to do to achieve this is to forward the
> session cookie from request to the urllib2() call (yes, it seems that
> urllib() is insufficient for this).
>
> I'm really struggling to get this working though.
>
> I've tried adding the header manually:
>             import urllib2
>             cookie = response.session_id_name + "=" +
> response.session_id
>             txheaders = {'Cookie' : cookie}
>             req = urllib2.Request(url, None, txheaders)
>             return urllib2.urlopen(req).read()
>
> But this seems to hang Web2Py completely!
> (Strangely it works fine when I direct at an unprotected external KML
> feed!)
>
> I also tried using cookielib:
>             cookie = request.cookies[response.session_id_name]
>             jar = cookielib.CookieJar()
>             jar.set_cookie(cookie)
>             handler = urllib2.HTTPCookieProcessor(jar)
>             opener = urllib2.build_opener(handler)
>             urllib2.install_opener(opener)
>             return urllib2.urlopen(url).read()
>
> However this fails because it wants cookie.domain to be defined (I
> tried defining it as "" but this fails too):
>
>             import Cookie, cookielib
>             cookie = Cookie.SimpleCookie()
>             cookie[response.session_id_name] = response.session_id
>             cookie['path'] = "/"
>             cookie['domain'] = ""
>
> This seems wrong anyway, as Set-Cookie is for Server->Client & I want
> Client->Server
>
> Any/All pointers very gratefully received :)
>
> Fran.

-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.

Reply via email to