通过 python 模拟 WMI 调用的 Windows?
我正在使用 PyWin32 从 django Web 应用程序以 python 方式对系统进行 WMI 调用。我的目标是允许用户通过 Web 界面将打印机添加到系统中。为此,我使用 win32print.AddPrinterConnection。
这在我的用户帐户下运行开发服务器效果很好。我可以添加所有我想要的打印机。然而,最终,这需要在作为 LocalSystem 帐户运行的 apache 下运行。
这是有问题的,原因有两个:
- LocalSystem 帐户根本没有网络权限,而且这是网络打印机。 AddPrinterConnection WMI 调用最终会进行将被禁止的 COM 调用。
- LocalSystem 帐户无权访问这些打印机所在的域。他们需要域帐户才能访问。
因此,我得出的结论是,我需要模拟域用户才能完成此任务。我已经使用此处找到的代码完成了此操作:
http://code.activestate.com/recipes/ 81402/
这似乎有效,因为我能够验证我是否已成功模拟调用代码。不幸的是,在模拟之后,我总是从 win32print.AddPrinterConnection API 调用中收到此错误:
Exception Type: error
Exception Value: (2, 'AddPrinterConnection', 'The system cannot find the file specified.')
您知道为什么会这样吗?
非常感谢! Pete
更新
玩了一下,我注意到如果我模拟的用户当前已登录系统,则 AddPrinterConnection API 调用会成功完成。一旦我注销该用户并在模拟该用户时重试该命令,我就会收到上述错误。
这是怎么回事?
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:
- 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.
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法帮助解决具体问题,但我确实知道,如果我必须在 Windows 上使用 Python 处理 WMI 内容,我肯定会使用 Tim Golden 的 Python WMI 模块而不是 pywin32。也许在使用该模块的文档/食谱或谷歌搜索中您可以找到解决方案。
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.