CherryPy not playing nicely with win32com?

I

infidel

I've been trying to get my CherryPy server to authenticate users
against our network. I've managed to cobble together a simple function
that uses our LDAP server to validate the username and password entered
by the user:

# ldap.py
from win32com.client import GetObject

ADS_SECURE_AUTHENTICATION = 1

def login_valid(username, password):
ldap = GetObject("LDAP:")
try:
ldap.OpenDSObject(
'LDAP://XXXXXXXX.US',
'XXXXXXXX\\' + username,
password,
ADS_SECURE_AUTHENTICATION
)
return True
except:
return False

This function works great if I call it from the interactive prompt:
True

But as soon as I have my CherryPy filter call this exact same function,
I get an "invalid syntax" COM error:

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\cherrypy\_cphttptools.py", line
77, in _run
applyFilters('beforeMain')
File "C:\Python24\Lib\site-packages\cherrypy\_cphttptools.py", line
461, in applyFilters
method()
File "filters\authenticate.py", line 29, in beforeMain
if ldap.login_valid(username, password):
File "K:\src\py\XXXXXXXX\filters\ldap.py", line 6, in login_valid
ldap = GetObject("LDAP:")
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py",
line 73, in GetObject
return Moniker(Pathname, clsctx)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py",
line 88, in Moniker
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147221020, 'Invalid syntax', None, None)

I don't get it. How can it be ok at the >>> prompt but invalid under
CherryPy?
 
I

Irmen de Jong

infidel said:
I've been trying to get my CherryPy server to authenticate users
against our network. I've managed to cobble together a simple function
that uses our LDAP server to validate the username and password entered
by the user:
[...]

moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147221020, 'Invalid syntax', None, None)

I don't get it. How can it be ok at the >>> prompt but invalid under
CherryPy?

Just an idea: because in CherryPy it is running in multithreading mode?
If you are using threads together with COM stuff, you will have to
add pythoncom.CoInitialize() and pythoncom.CoUninitialize() calls
in your code -- for each thread.

--Irmen
 
I

infidel

Just an idea: because in CherryPy it is running in multithreading mode?
If you are using threads together with COM stuff, you will have to
add pythoncom.CoInitialize() and pythoncom.CoUninitialize() calls
in your code -- for each thread.

That worked perfectly. Thanks a million!

I used to hate COM just because of our huge VB project. Now I can hate
it for this too!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top