使用Python將網絡共享映射到Windows驅動器的最佳方式是什麼? 此份額還需要用戶名和密碼。使用Python映射Windows驅動器的最佳方式是什麼?
回答
我會用IronPython和這篇文章:Mapping a Drive Letter Programmatically。或者你可以直接使用Win32 API。
這裏有幾個鏈接顯示使用win32net模塊應該提供您需要的功能。
http://docs.activestate.com/activepython/2.4/pywin32/html/win32/help/win32net.html http://www.blog.pythonlibrary.org/?p=20
我沒有一臺服務器在這裏在家進行測試,但也許你可以簡單地使用標準庫的子模塊來執行相應的NET USE命令?
看着從Windows NET USE HELP命令提示符下,看起來你應該能夠在net use命令同時輸入密碼和用戶ID映射驅動器。
快速測試在裸net use命令的解釋W/O映射東西:
>>> import subprocess
>>> subprocess.check_call(['net', 'use'])
New connections will be remembered.
There are no entries in the list.
0
>>>
假設你輸入必要的庫,這是在客戶端請求的RPC服務器的一部分服務器在本地映射驅動器...
#Small function to check the availability of network resource.
def isAvailable(path):
winCMD = 'IF EXIST ' + path + ' echo YES'
cmdOutPut = subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True).communicate()
return string.find(str(cmdOutPut), 'YES',)
#Small function to check if the mention location is a directory
def isDirectory(path):
winCMD = 'dir ' + path + ' | FIND ".."'
cmdOutPut = subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True).communicate()
return string.find(str(cmdOutPut), 'DIR',)
================從這裏檢查空格,這些是函數的一部分==== ========
def mapNetworkDrive(self, drive, networkPath, user, password):
#Check for drive availability
if isAvailable(drive) > -1:
#Drive letter is already in use
return -1
#Check for network resource availability
if isAvailable(networkPath) == -1:
print "Path not accessible: ", networkPath
#Network path is not reachable
return -1
#Prepare 'NET USE' commands
winCMD1 = 'NET USE ' + drive + ' ' + networkPath
winCMD2 = winCMD1 + ' ' + password + ' /User' + user
print "winCMD1 = ", winCMD1
print "winCMD2 = ", winCMD2
#Execute 'NET USE' command with authentication
winCMD = winCMD2
cmdOutPut = subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True).communicate()
print "Executed: ", winCMD
if string.find(str(cmdOutPut), 'successfully',) == -1:
print winCMD, " FAILED"
winCMD = winCMD1
#Execute 'NET USE' command without authentication, incase session already open
cmdOutPut = subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True).communicate()
print "Executed: ", winCMD
if string.find(str(cmdOutPut), 'successfully',) == -1:
print winCMD, " FAILED"
return -1
#Mapped on second try
return 1
#Mapped with first try
return 1
def unmapNetworkDrive(self, drive):
#Check if the drive is in use
if isAvailable(drive) == -1:
#Drive is not in use
return -1
#Prepare 'NET USE' command
winCMD = 'net use ' + drive + ' /DELETE'
cmdOutPut = subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True).communicate()
if string.find(str(cmdOutPut), 'successfully',) == -1:
#Could not UN-MAP, this might be a physical drive
return -1
#UN-MAP successful
return 1
好的,下面是另一種方法...
這是一個經過win32wnet後。讓我知道你在想什麼......
def mapDrive(drive, networkPath, user, password, force=0):
print networkPath
if (os.path.exists(drive)):
print drive, " Drive in use, trying to unmap..."
if force:
try:
win32wnet.WNetCancelConnection2(drive, 1, 1)
print drive, "successfully unmapped..."
except:
print drive, "Unmap failed, This might not be a network drive..."
return -1
else:
print "Non-forcing call. Will not unmap..."
return -1
else:
print drive, " drive is free..."
if (os.path.exists(networkPath)):
print networkPath, " is found..."
print "Trying to map ", networkPath, " on to ", drive, " ....."
try:
win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, drive, networkPath, None, user, password)
except:
print "Unexpected error..."
return -1
print "Mapping successful"
return 1
else:
print "Network path unreachable..."
return -1
而且取消映射,只需使用....
def unmapDrive(drive, force=0):
#Check if the drive is in use
if (os.path.exists(drive)):
print "drive in use, trying to unmap..."
if force == 0:
print "Executing un-forced call..."
try:
win32wnet.WNetCancelConnection2(drive, 1, force)
print drive, "successfully unmapped..."
return 1
except:
print "Unmap failed, try again..."
return -1
else:
print drive, " Drive is already free..."
return -1
大廈關閉@匿名的建議的:
# Drive letter: M
# Shared drive path: \\shared\folder
# Username: user123
# Password: password
import subprocess
# Disconnect anything on M
subprocess.call(r'net use m: /del', shell=True)
# Connect to shared drive, use drive letter M
subprocess.call(r'net use m: \\shared\folder /user:user123 password', shell=True)
我喜歡這個簡單的方法,特別是如果所有信息都是靜態的。
我有麻煩這一行的工作:
win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK,驅動器,networkPath,無,用戶名,密碼)
但成功與此:
win32wnet .WNetAddConnection2(1,'Z:',r'\ UNCpath \ share',None,'login','password')
如果你想映射當前登錄用戶,我認爲子進程解決你的問題。但是,您是否想要控制來自單個主帳戶的不同用戶的不同映射。你可以從windows的註冊表中做到這一點
這個想法是加載給定用戶的配置文件。
import win32api
import win32security
import win32profile
import win32netcon
import win32net
import win32netcon
import win32con
il = 'G'
m = '\\\\192.168.1.16\\my_share_folder'
usu = 'my_user'
cla = 'passwd'
#login the user
hUser = win32security.LogonUser(
usu,
None,
cla,
win32security.LOGON32_LOGON_NETWORK,
win32security.LOGON32_PROVIDER_DEFAULT
)
#load the profile
hReg = win32profile.LoadUserProfile (
hUser,
{"UserName" : usu}
)
#alter the regedit entries of usu
win32api.RegCreateKey(hReg, "Network")
hkey = win32api.RegOpenKey(hReg, "Network\\", 0, win32con.KEY_ALL_ACCESS)
win32api.RegCreateKey(hkey, il)
hkey = win32api.RegOpenKey(hReg, "Network\\%s" % il, 0, win32con.KEY_ALL_ACCESS)
win32api.RegSetValueEx(hkey, "ConnectionType", 0, win32con.REG_DWORD, 1)
win32api.RegSetValueEx(hkey, "DeferFlags", 0, win32con.REG_DWORD, 4)
win32api.RegSetValueEx(hkey, "ProviderName", 0, win32con.REG_SZ, "Red de Microsoft Windows")
win32api.RegSetValueEx(hkey, "ProviderType", 0, win32con.REG_DWORD, 131072)
win32api.RegSetValueEx(hkey, "RemotePath", 0, win32con.REG_SZ, m)
win32api.RegSetValueEx(hkey, "UserName", 0, win32con.REG_DWORD, 0)
- 1. 在java中實現映射器的最佳方式是什麼?
- 2. 映射映射鍵的最佳方式
- 3. 在Powershell中映射驅動器的「更好」方法是什麼?
- 4. 識別Google驅動器網址的最佳方法是什麼?
- 5. 什麼是將用戶映射到連接ID的最佳方式
- 6. 什麼是自動化Windows Azure部署的最佳方式?
- 7. 用濾波器映射多點數據集的最佳方法是什麼?
- 8. 在Python中使用「不等於」的最佳方式是什麼?
- 9. 什麼是使用python geotag jpeg-images的最佳方式?
- 10. 在python中使用web服務的最佳方式是什麼?
- 11. VMWare VMDK映射到Windows驅動器號
- 12. 共享Windows映射驅動器
- 13. Cygwin訪問Windows UNC映射驅動器
- 14. 使用MVC調用控制器的最佳方式是什麼?
- 15. 映射博客之間的鏈接連接的最佳方式是什麼?
- 16. 創建從SQL數據庫到GUI的映射的最佳方式是什麼?
- 17. 將需求映射到Redmine中的功能的最佳方式是什麼?
- 18. 使用JQuery動態添加HTML的最佳方式是什麼?
- 19. 什麼是在NHibernate中映射性別(性別)屬性的最佳方式
- 20. 在django項目中映射主要URL的最佳方式是什麼?
- 21. 在mongo中創建可變映射的最佳方式是什麼?
- 22. 將結果集映射到對象的最佳方式是什麼
- 23. 在XML和C#中實現多對多映射的最佳方式是什麼?
- 24. 將map/newyork映射到/area.jsp?id=1的最佳方式是什麼?
- 25. 對構建器使用@ConfigurationProperties的最佳方式是什麼?
- 26. 在Android上使用適配器的最佳方式是什麼?
- 27. 使用兩個比較器的最佳方式是什麼?
- 28. Python Win32 - DriveInfo在映射驅動器
- 29. SMB和Python與映射驅動器
- 30. 什麼是以編程方式使用Gmail的最佳方式?
使用'win32wnet.WNetAddConnection2'和'win32wnet.WNetCancelConnection2'是一個好主意。但是捕獲異常並返回一個返回碼看起來沒有必要。內置的例外已經爲您提供了良好的回報信息。另外,捕捉一個空的「except」不是很好的做法。 – twasbrillig 2016-01-07 21:56:20