Python HTTP digest authentication woes...

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

    Python HTTP digest authentication woes...

    I'm trying to access the XML version of my Tivo now playing list with
    python. It uses auth digest HTTP authentication. I could really use
    some help!

    I'm able to get this page using curl:
    curl --dump-header tivoHeaders --insecure --anyauth --user tivo:8000008
    "https://192.168.1.102/TiVoConnect?Com mand=QueryConta iner&Container= %2FNowPlaying&R ecurse=Yes"

    But

    when I use my python script, I get rejected:

    Error

    401
    Server: tivo-httpd-1:7.1b-01-2:140
    Set-Cookie: sid=DEC2D78EABF 48A6D; path=/; expires="Saturd ay,
    16-Feb-2013 00:00:00 GMT";
    WWW-Authenticate: Digest realm="TiVo DVR", nonce="FD08EF22 6909CA85", qop="auth"
    Content-Length: 31
    Content-Type: text/html
    Connection: close

    Digest realm="TiVo DVR", nonce="FD08EF22 6909CA85", qop="auth"

    I've scrounged for examples out there and the couple that I've found
    just don't seem to work for me..

    Here's one way I've tried:
    =============== =============== =======
    import urllib2

    theurl =
    "192.168.1. 102/TiVoConnect?Con tainer=%2FNowPl aying&Command=Q ueryContainer&R ecurse=Yes"
    print

    theurl

    protocol = 'https://'
    username = 'tivo'
    password = '8000008'

    passman = urllib2.HTTPPas swordMgrWithDef aultRealm()
    passman.add_pas sword(None, theurl, username, password)
    authhandler = urllib2.HTTPDig estAuthHandler( passman)
    opener = urllib2.build_o pener(authhandl er)
    urllib2.install _opener(opener)
    try:
    pagehandle = urllib2.urlopen (protocol + theurl)
    except IOError, e:
    if hasattr(e, 'code'):
    if e.code != 401:
    print 'We got another error'
    print e.code
    else:
    print "Error 401"
    print e.headers
    print e.headers['www-authenticate']
    =============== =============== =========

    I get 401 every time!
    This was taken from an example online almost verbatim, the only major
    thing I changed was HTTPBasicAuthHa ndler --> HTTPDigestAuthH andler. Any
    ideas or help would be greatly appreciated!

    Thanks,
    -John

  • ravelox

    #2
    re:Python HTTP digest authentication woes...

    I hacked this a little bit and it seems to do the trick.


    import urllib2

    theurl =
    "192.168.0. 25/TiVoConnect?Con tainer=%2FNowPl aying&Command=Q ueryContainer&R ecurse=Yes"
    print theurl

    protocol = 'https://'
    username = 'tivo'
    password = '1111111111'

    authhandler = urllib2.HTTPDig estAuthHandler( )
    authhandler.add _password("TiVo DVR",
    "192.168.0. 25", username, password)
    opener = urllib2.build_o pener(authhandl er)
    urllib2.install _opener(opener)
    try:
    pagehandle = urllib2.urlopen (protocol + theurl)
    print pagehandle.read lines()
    except IOError, e:
    if hasattr(e, 'code'):
    if e.code != 401:
    print 'We got another error'
    print e.code
    else:
    print "Error 401"
    print e.headers
    print e.headers['www-authenticate']


    Cheers

    Dave

    Comment

    • Fuzzyman

      #3
      Re: Python HTTP digest authentication woes...


      john wrote:[color=blue]
      > I'm trying to access the XML version of my Tivo now playing list with
      > python. It uses auth digest HTTP authentication. I could really use
      > some help!
      >
      > I'm able to get this page using curl:
      > curl --dump-header tivoHeaders --insecure --anyauth --user tivo:8000008
      > "https://192.168.1.102/TiVoConnect?Com mand=QueryConta iner&Container= %2FNowPlaying&R ecurse=Yes"
      >
      > But
      >
      > when I use my python script, I get rejected:
      > https://192.168.1.102/TiVoConnect?Co...er&Recurse=Yes
      > Error
      >
      > 401
      > Server: tivo-httpd-1:7.1b-01-2:140
      > Set-Cookie: sid=DEC2D78EABF 48A6D; path=/; expires="Saturd ay,
      > 16-Feb-2013 00:00:00 GMT";
      > WWW-Authenticate: Digest realm="TiVo DVR", nonce="FD08EF22 6909CA85", qop="auth"
      > Content-Length: 31
      > Content-Type: text/html
      > Connection: close
      >
      > Digest realm="TiVo DVR", nonce="FD08EF22 6909CA85", qop="auth"
      >
      > I've scrounged for examples out there and the couple that I've found
      > just don't seem to work for me..
      >
      > Here's one way I've tried:
      > =============== =============== =======
      > import urllib2
      >
      > theurl =
      > "192.168.1. 102/TiVoConnect?Con tainer=%2FNowPl aying&Command=Q ueryContainer&R ecurse=Yes"
      > print
      >[/color]

      Oh yeah - I didn't spot this before....

      theurl =
      "192.168.1. 102/TiVoConnect?Con ­tainer=%2FNowP laying&Command= Q­ueryContainer &Recurse=Yes "


      this includes the parameters - which it shouldn't.

      :-)

      Fuzzy


      Comment

      Working...