Search code examples
pythonhp-quality-center

urllib2 + Basic authentication + CookieJar


is there a way to use both openers to access a RESP Api from HP QC, or a different library i can use. im currently having trouble with a redirection error message "The HTTP server returned a redirect error that would lead to an infinite loop". but i can only use one opener whether to authenticate or cache cookies.

import json
import urllib2
from cookielib import CookieJar


SERVER = "http://server"


if __name__ == "__main__":
    cj = CookieJar()
    authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
    authpage = SERVER + "authentication-point/authenticate"
    authinfo.add_password(None, authpage, 'user', 'user')
    handler = urllib2.HTTPBasicAuthHandler(authinfo)
    cjopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(cjopener)
    myopener = urllib2.build_opener(handler)
    f = myopener.open(authpage)
    urllib2.install_opener(myopener)

    if(myopener):
        response = urllib2.urlopen(SERVER + "rest/is-authenticated")
        print response.info()
        print response.json()

Solution

  • or a different library i can use

    Requests.