What is the best way to map windows drives using P

2019-01-07 07:22发布

What is the best way to map a network share to a windows drive using Python? This share also requires a username and password.

8条回答
你好瞎i
2楼-- · 2019-01-07 07:27

Building off of @Anon's suggestion:

# 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)

I prefer this simple approach, especially if all the information is static.

查看更多
孤傲高冷的网名
3楼-- · 2019-01-07 07:28

I had trouble getting this line to work:

win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, drive, networkPath, None, user, password)

But was successful with this:

win32wnet.WNetAddConnection2(1, 'Z:', r'\UNCpath\share', None, 'login', 'password')

查看更多
Juvenile、少年°
4楼-- · 2019-01-07 07:30

If you want to map the current login user, i think subprocess solve your problem. But is you want to control different mappings for different users, from a single master account. You could do this from the register of windows

The idea is to load the profile of a given user.

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)
查看更多
手持菜刀,她持情操
5楼-- · 2019-01-07 07:31

I'd go with IronPython and this article : Mapping a Drive Letter Programmatically. Or you could use the Win32 API directly.

查看更多
孤傲高冷的网名
6楼-- · 2019-01-07 07:32

Okay, Here's another method...

This one was after going through win32wnet. Let me know what you think...

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

And to unmap, just use....

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楼-- · 2019-01-07 07:43

Here are a couple links which show use of the win32net module that should provide the functionality you need.

http://docs.activestate.com/activepython/2.4/pywin32/html/win32/help/win32net.html http://www.blog.pythonlibrary.org/?p=20

查看更多
登录 后发表回答