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

Hosted Zimbra Email Catches On With Managed Service Providers

Saturday, 19 July 2008
MSPmentor: "When it comes to hosted email, why zig when you can zag? That explains the strategy at EtomicMail, a managed service provider that’s hosting Zimbra — an open source email platf

An Open Source Seeing Eye Dog for Web Surfers

Saturday, 19 July 2008
LinuxInsider: "WebAnywhere is an open source, Web-based application that acts as a screen reader of sorts for blind people. Its developer, a student at the University of Washington, designed it so tha

Perl and Bash Versions Of Binary To Decimal Conversion Script

Saturday, 19 July 2008
The Linux and Unix Menagerie: "As promised, in yesterday's post on converting binary values to decimal in C, today we're going to follow up with straight-up ports to Perl and shell. Actually, they'll

E4X: JavaScript on Steroids

Saturday, 19 July 2008
IBM Developerworks: "E4X is designed to simplify the task of writing JavaScript code for XML. It is an efficient, powerful tool that you can use to interact with XML nodes and attributes. The primary

Linux 2.6.26 Kernel Benchmarks

Saturday, 19 July 2008
Phoronix: "Over the weekend the Linux 2.6.26 kernel was released. This quarterly update to the Linux kernel introduced Kernel-based Virtual Machine improvements, new One Laptop Per Child support, a ne

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.