spikesource hints'n'tips

Our Top Tags

                                       

Mailing List

Keep uptodate with the latest hints'n' tips as they are published by signing up to our mailing list.

Our RSS Feeds








Latest Linux News

Free Software in Education

Sunday, 24 May 2009
Free Software: "Free Software is political because it is strongly tied with a deep political question: that of education, and access to it."

A Great Multi-Platform Music Management Application: Jajuk

Sunday, 24 May 2009
Web Upd8: "Jajuk is a very comprehensive and feature rich Open Source music management software written in Java. The software program is therefore available for Windows, Linux and Apple Macintosh comp

Kiwi Imaging System - Forge your own operating system images in a matter of hours

Saturday, 23 May 2009
Dedoimedo: "Would you like to be able to create custom, ready-to-use operating system images that can be used as VMware images, Xen virtual machines or live DVDs or booted from USB sticks? Would you l

Linux Myth: No Simple, Easy Database Software

Saturday, 23 May 2009
The ERACC Web Log: "In this installment of my Linux Myth series I take aim at the GNU/Linux database nay-sayers, and shoot them down."

Finding the right open-source savvy lawyer

Saturday, 23 May 2009
Practical Technology: "Some days, like it or not, you need a lawyer. For most business purposes, picking the right law firm isn’t usually that big of a deal"

Latest Digg Entries

Http client using digest authentication (in python)

posted Wednesday, 14 September 2005
Authenticating using http digest isn't too hard. See wikipedia for an overview. But you don't want to implement it yourself if you use a programming language with "batteries included". The python module urllib2 provides one with access to http digest authentication, but it's use isn't straightforward (or documented too well). The key to using urllib is created an HTTPDigestAuthHandler and calling the add_password method on it. To that method pass the realm, url (NOT the uri, the complete url, this is where I got a little stuck), username and password.

See the unittest below (especially the connectWithDigestAuth function).

Sample code

import unittest
import urllib2

def connectWithDigestAuth(url, username, pw, realm):
    authhandler = urllib2.HTTPDigestAuthHandler()
    authhandler.add_password(realm, url, username, pw)
    opener = urllib2.build_opener(authhandler)
    urllib2.install_opener(opener)
    pagehandle = urllib2.urlopen(url)
    lines =  pagehandle.readlines()
    return lines


class testLinebreak(unittest.TestCase):
    def testAuth(self):
        url = "http://localhost/authTest/index.html"
        username = "matt"
        password = "password"
        realm = "/home/spike"
        contents = connectWithDigestAuth(url, username, password, realm)
        self.failUnless('You should be authenticated!\n' in contents)


    def testBadAuth(self):
        url = "http://localhost/authTest/index.html"
        username = "BadUser"
        password = "password"
        realm = "/home/spike"
        self.assertRaises(urllib2.HTTPError, connectWithDigestAuth,url, username, password, realm)

    def testNoAuth(self):
        "a test to make sure this location is protected"
        url = "http://localhost/authTest/index.html"
        req = urllib2.Request(url)
        self.assertRaises(urllib2.HTTPError, urllib2.urlopen, req)
        
if __name__=="__main__":
    unittest.main()

tags:    

links: digg this    del.icio.us    technorati    




1. Nong left...
Thursday, 25 May 2006 2:16 am :: http://samba.math.science.cmu.ac.th

username&password


2. tik left...
Tuesday, 13 June 2006 8:55 am

http://samba.math.science.cmu.ac.th


Related Posts

Putting a MySQL query to sleep

Tuesday, 10 October 2006 8:05 A GMT
There are a number of reasons why you would want to put a MySQL query to sleep, here is how.

Resetting the root password on MySQL and managing legacy password access

Friday, 18 August 2006 3:16 P GMT
If you need to use an old client library with MySQL 5, or you need to reset the root mysql password, this entry looks at both.

Http client using digest authentication (in python)

Wednesday, 14 September 2005 8:00 A GMT
To test mod_auth_digmysql, you needed to test that it works so here are some testcases in python.

Introduction to using MySQLdb with python

Saturday, 10 September 2005 9:31 A GMT
MySQLdb is the Python DB API-2.0 interface and this blog looks at the using the MySQL from Python

How to backup and import a MySQL InnoDB database

Tuesday, 16 August 2005 10:56 A GMT
Details how to properly backup and then restore a Mysql database running Innodb as oppose to MyISAM.