[英]How can I add Windows Credentials using Python?
我正在嘗試創建一個程序,該程序會自動將網絡快捷方式安裝到我的桌面上並下載驅動程序(這是我的工作的打印服務器),但是要在安裝快捷方式后訪問該快捷方式,我需要在下面輸入我的工作憑證打印機網絡的域。 我嘗試使用Python的密鑰環庫,但未成功,我還嘗試使用win32wnet.WNETADDCONNECTION2()(已在多個論壇上看到該發布),但對我來說也不成功。
這是當前的代碼
import os, winshell, keyring, win32wnet
from win32com.client import Dispatch
#Add Windows Credentials
#**************************
url = r'\\LINKTOTHENETWORK'
win32wnet.WNetAddConnection2(0, None, url, None, USERNAME, PASSWORD)
keyring.set_password(url, USERNAME, PASSWORD)
keyring.get_password(url, USERNAME)
#**************************
# This is where I am having troubles.
# Create the shortcut
desktop = winshell.desktop()
path = os.path.join(desktop, "MYLINK.lnk")
target = url
# Set path and save
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.save()
# Open the shortcut
os.startfile(target)
在我的完整程序中,我有一個使用Kivy的界面,詢問我的用戶名和密碼,然后單擊“安裝”按鈕,它將域添加到我的用戶名(域\\用戶名)中。 使用密鑰環可以正確地顯示它,只是在錯誤的區域,這樣就不成問題了,我只是找不到添加Windows憑據而不是常規憑據的方法。
我在Windows 10計算機上使用python2.7。
如果有人知道我可以使用圖書館或其他很棒的方法。 謝謝!
我很確定您可以通過創建.bat文件並在程序中執行該文件來實現這一目標。
對於前
def bat_create_with_creds(id, pwd):
"""create Batch file to init printer domain creds with variables"""
auth_print= open("auth_print.bat", "w")
auth_print.write("NET USE \\printserverip /USER:%"+id+"% %"+pwd+"%")
auth_print.close()
對於您的特定用例,我還沒有做到這一點,但是它對於init來說效果很好。 例如,使用Windows mstsc的rdp會話。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.