Python - How to share Windows with username and password

I would like to access the Windows shared folder (e.g.. \ Backupserver \ backups) from a Python script. Share is protected by username and password. How to open this resource using a username and password and, for example, list its contents?

+5
source share
4 answers

Why don't you mount the linked resource with

NET USE 

team?

The call NET USEfrom the subprocess module is direct.

+1
source

pywin32 (Python Windows Extensions), Windows win32wnet. win32wnet.WNetAddConnection2() .

WNetAddConnection2(NetResource, Password, UserName, Flags)

. .

, , , .

+6

Full example for "NET USE":

backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)

if backup_storage_available:
    logger.info("Backup storage already connected.")
else:
    logger.info("Connecting to backup storage.")

    mount_command = "net use /user:" + BACKUP_REPOSITORY_USER_NAME + " " + BACKUP_REPOSITORY_PATH + " " + BACKUP_REPOSITORY_USER_PASSWORD
    os.system(mount_command)
    backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)

    if backup_storage_available:
        logger.fine("Connection success.")
    else:
        raise Exception("Failed to find storage directory.")
+6
source

A good library that wraps the "net use" command:

http://covenanteyes.imtqy.com/py_win_unc/

+1
source

All Articles