Python HTTP digest authentication woes...

Discussion in 'Python' started by john, Jul 17, 2005.

  1. john

    john Guest

    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?Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes"

    But

    when I use my python script, I get rejected:
    https://192.168.1.102/TiVoConnect?Container=/NowPlaying&Command=QueryContainer&Recurse=Yes
    Error

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

    Digest realm="TiVo DVR", nonce="FD08EF226909CA85", 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?Container=%2FNowPlaying&Command=QueryContainer&Recurse=Yes"
    print

    theurl

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

    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, theurl, username, password)
    authhandler = urllib2.HTTPDigestAuthHandler(passman)
    opener = urllib2.build_opener(authhandler)
    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 HTTPBasicAuthHandler --> HTTPDigestAuthHandler. Any
    ideas or help would be greatly appreciated!

    Thanks,
    -John
     
    john, Jul 17, 2005
    #1
    1. Advertising

  2. john

    ravelox Guest

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


    import urllib2

    theurl =
    "192.168.0.25/TiVoConnect?Container=%2FNowPlaying&Command=QueryContainer&Recurse=Yes"
    print theurl

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

    authhandler = urllib2.HTTPDigestAuthHandler()
    authhandler.add_password("TiVo DVR",
    "192.168.0.25", username, password)
    opener = urllib2.build_opener(authhandler)
    urllib2.install_opener(opener)
    try:
    pagehandle = urllib2.urlopen(protocol + theurl)
    print pagehandle.readlines()
    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
     
    ravelox, Aug 8, 2005
    #2
    1. Advertising

  3. john

    Fuzzyman Guest

    john wrote:
    > 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?Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes"
    >
    > But
    >
    > when I use my python script, I get rejected:
    > https://192.168.1.102/TiVoConnect?Container=/NowPlaying&Command=QueryContainer&Recurse=Yes
    > Error
    >
    > 401
    > Server: tivo-httpd-1:7.1b-01-2:140
    > Set-Cookie: sid=DEC2D78EABF48A6D; path=/; expires="Saturday,
    > 16-Feb-2013 00:00:00 GMT";
    > WWW-Authenticate: Digest realm="TiVo DVR", nonce="FD08EF226909CA85", qop="auth"
    > Content-Length: 31
    > Content-Type: text/html
    > Connection: close
    >
    > Digest realm="TiVo DVR", nonce="FD08EF226909CA85", 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?Container=%2FNowPlaying&Command=QueryContainer&Recurse=Yes"
    > print
    >


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

    theurl =
    "192.168.1.102/TiVoConnect?Con­tainer=%2FNowPlaying&Command=Q­ueryContainer&Recurse=Yes"


    this includes the parameters - which it shouldn't.

    :)

    Fuzzy
    http://www.voidspace.org.uk/python
     
    Fuzzyman, Aug 8, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EJ
    Replies:
    0
    Views:
    412
  2. John Reese
    Replies:
    3
    Views:
    428
    John Reese
    Jan 3, 2005
  3. Tammy Mc
    Replies:
    3
    Views:
    217
    Tammy Mc
    Oct 1, 2006
  4. myalo
    Replies:
    4
    Views:
    1,308
    A. Sinan Unur
    Nov 28, 2007
  5. Replies:
    2
    Views:
    333
    Julian Cromarty
    Jun 26, 2013
Loading...

Share This Page