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

20 Beautiful Dark Themes for Gnome and Ubuntu

Tuesday, 19 August 2008
Cats Who Code: "Some weeks ago, I shown you a list of 30 themes for enhancing your Gnome desktop."

OpenSolaris: a Linux Admin's View

Tuesday, 19 August 2008
Linux Format: "Sun is battling hard to break into the open source operating system world with OpenSolaris. Juliet Kemp takes it for a test-drive, sampling its unique features and seeing how it fares a

What's That They Say About Assumptions?

Tuesday, 19 August 2008
Blog of Helios: "Sometimes Linux isn't the answer."

The Brampton Factor: Analysts Fail on Open Source

Tuesday, 19 August 2008
IT Pro: "For open source software to achieve its full potential, people's perceptions must change. Yet how can that happen when open source is so woefully neglected by analysts, asks Martin Brampton."

Why Red Hat Invested In JBoss Instead Of Linux Desktops

Tuesday, 19 August 2008
The VAR Guy: "Ever wonder why Red Hat spends so much time focused on the JBoss middleware market and so little time trying to make Linux a desktop standard? The answer involves some simple but startli

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.