views:

385

answers:

1

I'm using PyWin32 to make WMI calls to the system in python from my django web application. My goal is to allow users to add printers to the system via a web interface. To do this, I'm using win32print.AddPrinterConnection.

This works well running the development server under my user account. I can add all the printers I want. However, eventually, this will need to run under apache which runs as the LocalSystem account.

This is problematic for two reasons:

  1. The LocalSystem account has no network privileges at all, and this is a network printer. The AddPrinterConnection WMI call eventually makes a COM call that will be disallowed.
  2. The LocalSystem account has no access to the domain these printers are on. They require a domain account to access.

Therefore, I've come to the conclusion that I need to impersonate domain user(s) to accomplish this task. I've done so using the code found here:

http://code.activestate.com/recipes/81402/

This seems to work as I'm able to verify that I've successfully impersonated the calling code. Unfortunately, after impersonation I always get this error from the win32print.AddPrinterConnection API call:

Exception Type: error 
Exception Value: (2, 'AddPrinterConnection', 'The system cannot find the file specified.')

Do you have any idea why this may be?

Thanks a bunch! Pete

Update

Playing around, I noticed the the AddPrinterConnection API call completes successfully if the user that I'm impersonating is currently logged into the system. Once I log that user out and retry the command while impersonating that user, I get the error stated above.

What is going on here?

A: 

I can't help with the specific problem, but I do know that if I had to work with WMI stuff on Windows, with Python, I would definitely reach for Tim Golden's Python WMI module instead of pywin32. Perhaps in the documentation/cookbook or Google searches using that module you can find a solution.

Peter Hansen