[python-win32] Python Win32 and Active Directory
Mark Hammond
mhammond at keypoint.com.au
Mon Mar 15 18:24:54 EST 2004
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