【问题标题】:Accessing Windows Fileshares with a domain user account in Python在 Python 中使用域用户帐户访问 Windows 文件共享
【发布时间】:2021-12-26 23:46:53
【问题描述】:

我正在开发一种工具来自动生成一些文档并将它们存储在文件共享中。我有一个具有所需访问权限的服务帐户,我正在尝试了解如何最好地指定我想在访问文件共享时使用该帐户。

这是我尝试访问我的测试文件的相关测试代码:

database_path = "\\\\testserver\\testfile.txt"
try:
    with open(database_path, "r") as testfile:
        lines = testfile.readlines()
        print(lines)
        testfile.close()
except FileNotFoundError:
    print("Couldn't access the test file.")

我通过 gitlab 通过 linux 运行程序运行此程序,因此捎带现有的域用户帐户并不是一个理想的解决方案。

有什么想法吗?

【问题讨论】:

    标签: python active-directory gitlab gitlab-ci


    【解决方案1】:

    您可以使用 cifs-utils 和 mount 简单地将文件共享挂载到 Linux 文件系统上

    sudo mount -t cifs -o username=<win_share_user>,password=<password> //<WIN_SHARE_HOST>/<share_name> /mnt/my_share
    

    然后在 Python 中,只需打开您想要的文件,例如open('/mnt/my_share/path/to/file')

    你需要在你的 linux 机器上安装 cifs-utils:

    apt install cifs-utils 用于 ubuntu/debian 或
    yum install cifs-utils 用于 Fedora 或
    apk add cifs-utils 用于 alpine

    或者,您可以使用任何支持 samba(Windows 使用的底层标准)文件共享协议的 Python 包,例如 fs.smbfs

    import fs
    smb_fs = fs.open_fs('smb://username:password@SAMBAHOSTNAME:port/share')
    

    【讨论】:

    • 你就是那个男人。我以某种方式完全忘记了SMB。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2020-03-27
    • 2016-06-16
    • 1970-01-01
    相关资源
    最近更新 更多