[python-win32] Python Win32 and Active Directory

Bill boober95 at rogers.com
Mon Mar 15 19:19:13 EST 2004


Very interesting ... so I removed the references to 'AD', then I had to 
change the last reference to add quotes so it is 'pw' since then it was 
undefined (of course this ain't right, but I just want to see it work).

That got me to the last line, added an import of 'ldap' and it started to 
make more sense.  Now I get a 'com' error, but I'm not connected to the net 
with the AD server, so I will have to try it out in the morning, but it 
certainly looks better than it did before the changes!

I think I've been mislead though, I thought the example (it was taken from 
the online help for active directory), was a working bit of code, but I'm 
beginning to think it was cut from something larger and not tested.

I'll let you know what happens in the morning with this.

/bill


 On March 15, 2004 06:24 pm, Mark Hammond wrote:
> I'm afraid I don't know the ActiveDirectory API well enough to know for
> sure, but:
>
> The code in question is:
>
> def opends(loc,server=''):
>     '''automatically buoild ldap string and authenticate'''
>     ldap=win32com.client.Dispatch('ADsNameSpaces').getobject("","LDAP:")
>     ldap_main_loc='OU=people,DC=ad,DC=company,DC=state,DC=oh,DC=us'
>     ldap_auth='CN=admin_account,'+ldap_main_loc
>
>     #if there is no ","  then they are not providing a full url
>     #so append the standard url for it
>     #if there is no '=', assume they want a cn
>     if loc.find(',')==-1:
>         if loc.find('=')==-1: loc='cn='+loc+','+Ad.ldap_main_loc
>         else: loc=loc+','+Ad.ldap_main_loc
>     if loc.find('LDAP://')==-1: loc='LDAP://'+loc
>
>     return ldap.OpenDSObject(loc,Ad.ldap_auth,Ad.pw,1)
>
> And the traceback:
>   File "<interactive input>", line 1, in ?
>   File "C:\Shared\project\opends.py", line 29, in opends
>     if loc.find('=')==-1: loc='cn='+loc+','+Ad.ldap_main_loc
> NameError: global name 'Ad' is not defined
>
> Looking at the line:
>
>     if loc.find('=')==-1: loc='cn='+loc+','+Ad.ldap_main_loc
>
> It is trying to reference an 'Ad' object, to fetch its ldap_main_loc
> attribute.  However, the code does not define an 'Ad' object - hence Python
> raises the NameError.  It is telling you that you have referenced an object
> before you have defined it.
>
> I'm not sure what 'Ad' is supposed to be.  However, you do have a variable
> called 'ldap_main_loc', so maybe this is what you meant - ie:
>
>         if loc.find('=')==-1: loc='cn='+loc+','+ldap_main_loc
>
> I note that most lines past that also reference 'Ad' - but all references
> have a local variable of the exact same name.  It looks like you simply
> want to remove all 'Ad.' references.
>
> Mark.
>
> > -----Original Message-----
> > From: python-win32-bounces at python.org
> > [mailto:python-win32-bounces at python.org]On Behalf Of Bill
> > Sent: Tuesday, 16 March 2004 4:07 AM
> > To: Mark Hammond; 'Bill'; python-win32 at python.org
> > Subject: Re: [python-win32] Python Win32 and Active Directory
> >
> >
> > Mark,
> >
> > Thanks for getting back to me.  Attached is the example I
> > tried to run on my
> > windows xp machine.  What I want is authentication to a
> > windows server, but I
> > haven't gotten very far, but I'm new at this so I expect I've
> > made an error?
> > (I did fix the problem with the doc string which wasn't
> > closed though.)
> >
> > /bill
> >
> > On March 15, 2004 04:05 am, Mark Hammond wrote:
> > > > Has anyone tried using Active Directory thru the Python Win32 API?
> > > >
> > > > I want to authenticate to AD and tried the simple example in
> > > > the online
> > > > docs for 'opends' but alway get 'AD' is an undefined symbol.
> > > > I may have
> > > > missed something obvious, but I sure can't see what it is :-(
> > >
> > > Can you post your complete sample code, and the complete
> >
> > traceback you get?
> >
> > > Mark.



More information about the Python-win32 mailing list