Proxy + cookie: urllib2? httplib2? Other?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gilles Ganault

    Proxy + cookie: urllib2? httplib2? Other?

    Hello

    I need a library that supports both going out through a proxy, and
    handling cookies automagically (the server uses a sessionID to keep
    track of the user).

    UrlLib2 supports the proxy part, httplib2 supports the cookie part
    but... Google didn't return code that shows both uses in the same
    library. Does someone know if either of those, or yet some other
    library would support both features?

    Thank you.
  • Gilles Ganault

    #2
    Re: Proxy + cookie: urllib2? httplib2? Other?

    On Fri, 14 Nov 2008 16:03:13 +0100, Gilles Ganault <nospam@nospam. com>
    wrote:
    >I need a library that supports both going out through a proxy, and
    >handling cookies automagically (the server uses a sessionID to keep
    >track of the user).
    For those interested, it seems like a good combination is urllib2 +
    cookielib:

    ==========
    import urllib2
    import cookielib

    #Set up proxy infos
    proxy_info = { 'host' : 'localhost','po rt' : 8119}
    proxy_support = urllib2.ProxyHa ndler({"http" :
    "http://%(host)s:%(port )d" % proxy_info})

    #Set up cookie handler
    cj = cookielib.Cooki eJar()

    #Create UrlLib2 opener
    opener =
    urllib2.build_o pener(proxy_sup port,urllib2.HT TPCookieProcess or(cj))
    urllib2.install _opener(opener)

    headers = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows
    NT)' }
    url = 'http://www.acme.com'
    req = urllib2.Request (url, None, headers)

    response = urllib2.urlopen (req).read()

    for index, cookie in enumerate(cj):
    print index, ' : ', cookie
    ==========

    HTH,

    Comment

    Working...