aqu*_*qua 20
建立@Anon的建议:
# 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)
Run Code Online (Sandbox Code Playgroud)
我更喜欢这种简单的方法,特别是如果所有信息都是静态的.
小智 7
好的,这是另一种方法......
这是在经历了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
Run Code Online (Sandbox Code Playgroud)
要取消映射,只需使用....
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
Run Code Online (Sandbox Code Playgroud)
我家里没有可以测试的服务器,但也许您可以简单地使用标准库的子进程模块来执行适当的 NET USE 命令?
从 Windows 命令提示符查看 NET HELP USE,看起来您应该能够在 net use 命令中输入密码和用户 ID 来映射驱动器。
在裸网使用命令的解释器中进行快速测试,无需映射内容:
>>> import subprocess
>>> subprocess.check_call(['net', 'use'])
New connections will be remembered.
There are no entries in the list.
0
>>>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
45811 次 |
最近记录: |