简体   繁体   English

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

[英]Cannot access file on Samba server via Python

I'm trying to access a file on our Samba server using Python. 我正在尝试使用Python访问我们的Samba服务器上的文件。 I found out I need to use a Samba client for this, so I started using PySmbClient. 我发现我需要为此使用Samba客户端,因此我开始使用PySmbClient。 Even though there are many examples online of how to do this, mine just does not want to work. 即使在线上有许多示例说明如何执行此操作,我的也不想这样做。 See below. 见下文。

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

This produces the following error: 这将产生以下错误:

OSError: [Errno 2] No such file or directory

with the following trace: 具有以下跟踪:

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

I've read and implemented many "solutions", but so far nothing has worked for me. 我已经阅读并实现了许多“解决方案”,但是到目前为止,对我来说没有任何作用。 I can access the Samba server with the given credentials through my file manager just fine, so I know those values should be fine. 我可以通过文件管理器使用给定的凭据访问Samba服务器,因此我知道这些值应该很好。 I even spoke to our sys admin and he doesn't know what could be wrong. 我什至与我们的系统管理员交谈,他不知道有什么问题。

It must be more than the simple code I wrote. 它一定不仅仅是我编写的简单代码。 Do you think there's an issue on the server side of things? 您是否认为服务器方面存在问题? Something with the values I input into SambaClient? 与我输入SambaClient的值有关吗? At this point I'm pretty much open to anything that leads to a solution. 在这一点上,我对任何导致解决方案的事情都持开放态度。

Here's some code that works for me, transferring a file from a Linux Samba share to my Windows laptop. 这是一些对我有用的代码,它将文件从Linux Samba共享传输到我的Windows笔记本电脑。 It's also known to work fine in the other direction (Linux client, Windows server). 还可以在其他方向(Linux客户端,Windows服务器)正常工作。

I'm using the pysmb library version 1.1.19 (the latest) and Python 2.7.1. 我正在使用pysmb库版本1.1.19(最新版本)和Python 2.7.1。

See the pysmb site for the pysmb package; 请参阅pysmb网站以获取pysmb软件包; I actually downloaded and installed it directly from its tarball and setup.py, as pip was throwing an error. 实际上,我直接从其tarball和setup.py下载并安装了它,因为pip抛出错误。

The pysmb package is less user-friendly but it does work well for Windows clients. pysmb软件包对用户的友好程度较低,但对于Windows客户端来说效果很好。

I set up a share called "my_share" on the Linux machine for user "edwards" using the following entry in smb.conf: 我使用smb.conf中的以下条目在Linux计算机上为用户“ edwards”设置了一个名为“ my_share”的共享:

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

And then used the following code to list the files on the share, and download a file called "rti_license.dat" to my laptop: 然后使用以下代码列出共享中的文件,并将名为“ 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()

Note that the server name must be correct or the connection won't work (from a Linux machine it's the output of the hostname command) 请注意,服务器名称必须正确,否则连接将无法正常工作(在Linux计算机上,这是hostname命令的输出)

Hope this may be useful. 希望这可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM