# 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)
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
要取消Map,只需使用....
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
7条答案
按热度按时间62o28rlo1#
根据@Anon的建议:
我更喜欢这种简单的方法,特别是如果所有的信息都是静态的。
des4xlb02#
好吧,这是另一个方法…
这一次是通过win32wnet。让我知道你的想法。。
要取消Map,只需使用....
9udxz4iz3#
我在家里没有一个服务器来测试,但是也许您可以简单地使用标准库的子进程模块来执行适当的NETUSE命令?
从Windows命令提示符查看NETHELP USE,看起来您应该能够在net use命令中输入密码和用户id来Map驱动器。
在裸网解释器中快速测试使用命令不带Map的东西:
at0kjp5o4#
假设您导入必要的库,这是RPC服务器的一部分,客户端请求服务器在本地Map驱动器...
=================从这里检查白色,这些是函数的一部分
pkwftd7m5#
子过程的替代方案:
或者
mo49yndu6#
如果你想Map当前的登录用户,我想子进程可以解决你的问题。但是您希望从一个主帐户为不同的用户控制不同的Map。您可以从Windows的注册表中执行此操作
这个想法是加载给定用户的配置文件。
u5i3ibmn7#
我有麻烦让这条线工作:
但他成功地做到了: