Python and ADSI (more ADSI really)

Paul Robinson paul.robinson at quantisci.co.uk
Wed Jan 5 11:35:36 EST 2000


On the subject of ADSI I have just returned to a problem I was having
several months ago...

I was using ADSI, specifically the WinNT provider. I was using Python as
it happens, but I have also tried a VBScipt version with exactly the
same results.
Any creation of an ADSI object was taking around 15 seconds. I could
find no mention of this in the SDK documentation or MSDN.
I started my searching for an answer again and found this useful nugget
at:
http://x33.deja.com/getdoc.xp?AN=516935348

Thus transforming my code from completely un-usable to really quite
useful....

E.g.
the code below (t2.cgi) produces the following output:

D:\Temp\cgi>python t2.cgi
Fast way: 0.039999961853
Slow way: 15.0319999456

So as you can see that is a significant speedup!!!
I hope this is useful to any ADSI users (and potential ADSI users)....

I thought I'd share this to alleviate (at least one of) the potential
problems that anyone could possibly have in the future.

	Paul.


#######################################################################
from time import time
import win32com.client, pywintypes

NS = win32com.client.Dispatch('ADSNameSpaces')

def bindUserWinNt(computerName, bindUser, userName=None,
                  passwd=None):
    if not userName:
        try:com = NS.getobject('','WinNT://' + computerName +
                               '/'+ bindUser +',user')
        except pywintypes.com_error, why:
            com = None
            print why[1], computerName, bindUser
    else:
        dso = NS.getobject('','WinNT:')
        try:com = dso.OpenDSObject('WinNT://' + computerName + '/'+
                                   bindUser + ',user', userName,
                                   passwd, 1)
        except pywintypes.com_error, why:
            com= None
            print why[1], computerName, userName
    return com

def bindUserWinNt2(computerName, bindUser, domain,
                   userName=None, passwd=None):
    if not userName:
        try:com = NS.getobject('','WinNT://' + domain+'/'+
                               computerName + '/'+ bindUser +',user')
        except pywintypes.com_error, why:
            com = None
            print why[1], computerName, bindUser
    else:
        dso = NS.getobject('','WinNT:')
        try:com = dso.OpenDSObject('WinNT://' + domain + '/' +
                                   computerName + '/' + bindUser +
                                   ',user', userName, passwd, 1)
        except pywintypes.com_error, why:
            com= None
            print why[1], computerName, userName
    return com

if __name__ == '__main__':
    t = time()
    u = bindUserWinNt2('pc-pdr2', 'test', 'isdg')
    print 'Fast way: '+`time()-t`
    t = time()
    u = bindUserWinNt('pc-pdr2', 'test')
    print 'Slow way: '+`time()-t`
#######################################################################




More information about the Python-list mailing list