Proxy + cookie: urllib2? httplib2? Other?

Discussion in 'Python' started by Gilles Ganault, Nov 14, 2008.

  1. 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, Nov 14, 2008
    #1
    1. Advertisements

  2. For those interested, it seems like a good combination is urllib2 +
    cookielib:

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

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

    #Set up cookie handler
    cj = cookielib.CookieJar()

    #Create UrlLib2 opener
    opener =
    urllib2.build_opener(proxy_support,urllib2.HTTPCookieProcessor(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,
     
    Gilles Ganault, Nov 14, 2008
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.