gpt4 book ai didi

python - 无法通过Python访问Samba服务器上的文件

转载 作者:行者123 更新时间:2023-11-28 21:40:29 24 4
gpt4 key购买 nike

我正在尝试使用 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 中的值吗?在这一点上,我对任何能带来解决方案的事情都持开放态度。

最佳答案

下面是一些适用于我的代码,可将文件从 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 命令的输出)

希望这可能有用。

关于python - 无法通过Python访问Samba服务器上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45552239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com