【问题标题】:Cannot access file on Samba server via Python无法通过 Python 访问 Samba 服务器上的文件
【发布时间】:2018-01-15 01:47:07
【问题描述】:

我正在尝试使用 Python 访问我们 Samba 服务器上的文件。我发现我需要为此使用 Samba 客户端,所以我开始使用 PySmbClient。尽管网上有很多关于如何做到这一点的例子,但我的只是不想工作。见下文。

smb = smbclient.SambaClient(server="192.168.0.320", share="DATA", domain="WORKGROUP",username="admin", password="abc123")
f = smb.open('test.json', 'r')

这会产生以下错误:

OSError: [Errno 2] No such file or directory

带有以下痕迹:

Traceback (most recent call last):
  File "create_dataset.py", line 35, in <module>
    f = smb.open('serverSaver.txt', 'r')
  File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 408, in open
    f = _SambaFile(self, path, mode)
  File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 448, in __init__
    connection.download(remote_name, self._tmp_name)
  File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 393, in download
    result = self._runcmd('get', remote_path, local_path)
  File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 184, in _runcmd
    return self._raw_runcmd(fullcmd)
  File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 168, in _raw_runcmd
    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
    raise child_exception

我已经阅读并实施了许多“解决方案”,但到目前为止,没有什么对我有用。我可以通过我的文件管理器使用给定的凭据访问 Samba 服务器,所以我知道这些值应该没问题。我什至与我们的系统管理员交谈过,他不知道可能出了什么问题。

它一定比我写的简单代码还要多。你认为服务器端有问题吗?我输入到 SambaClient 中的值是什么?在这一点上,我对任何可以找到解决方案的事情都持开放态度。

【问题讨论】:

    标签: python samba fileserver libsmbclient


    【解决方案1】:

    这里有一些对我有用的代码,将文件从 Linux Samba 共享传输到我的 Windows 笔记本电脑。它也可以在另一个方向(Linux 客户端、Windows 服务器)上正常工作。

    我正在使用 pysmb 库版本 1.1.19(最新)和 Python 2.7.1。

    请参阅the pysmb site 获取 pysmb 包;我实际上是直接从它的 tarball 和 setup.py 下载并安装了它,因为 pip 抛出了一个错误。

    pysmb 包的用户友好性较差,但它确实适用于 Windows 客户端。

    我使用 smb.conf 中的以下条目在 Linux 机器上为用户“edwards”设置了一个名为“my_share”的共享:

    [my_share]
    path = /home/edwards
    valid_users = edwards
    read only = no
    guest ok = yes
    browseable = yes
    

    然后使用以下代码列出共享上的文件,并将名为“rti_license.dat”的文件下载到我的笔记本电脑:

    import tempfile
    import smb
    import shutil
    
    from smb.SMBConnection import SMBConnection
    
    share_name          = "my_share"
    user_name           = "edwards"
    password            = "######"             # secret :-)
    local_machine_name  = "laptop"             # arbitrary
    server_machine_name = "edwards-Yocto"      # MUST match correctly
    server_IP           = "192.162.2.1"        # as must this            
    
    # create and establish connection
    conn = SMBConnection(user_name, password, local_machine_name, server_machine_name, use_ntlm_v2 = True)
    
    assert conn.connect(server_IP, 139)
    
    # print list of files at the root of the share
    files = conn.listPath(share_name, "/") 
    for item in files:
        print item.filename
    
    # check if the file we want is there
    sf = conn.getAttributes(share_name, "rti_license.dat")
    print sf.file_size
    print sf.filename
    
    # create a temporary file for the transfer
    file_obj = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
    file_name = file_obj.name
    file_attributes, copysize = conn.retrieveFile(share_name, "rti_license.dat", file_obj)
    print copysize
    file_obj.close()
    
    # copy temporary file 
    shutil.copy(file_name, "rti_license.dat")
    
    # close connection
    conn.close()
    

    请注意,服务器名称必须正确,否则连接将无法正常工作(在 Linux 机器上,它是 hostname 命令的输出)

    希望这可能有用。

    【讨论】:

    • 我切换到 pysmb 并立即开始工作。感谢您的帮助。
    • 感谢这篇文章!很难找到一个下降的 pysmb 例子
    猜你喜欢
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多