2017-08-07 252 views
0

我試圖使用Python訪問我們的Samba服務器上的文件。我發現我需要爲此使用Samba客戶端,所以我開始使用PySmbClient。儘管網上有很多關於如何做到這一點的例子,但我只是不想工作。見下文。無法通過Python訪問Samba服務器上的文件

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中的值有什麼關係?在這一點上,我對任何導致解決方案的東西都非常開放。

回答

1

下面是一些適用於我的代碼,將文件從Linux Samba共享傳輸到我的Windows筆記本電腦。在另一個方向(Linux客戶端,Windows服務器)也可以正常工作。

我使用pysmb庫1.1.19版(最新版)和Python 2.7.1。

請參閱the pysmb site pysmb包;我實際上是從它的tarball和setup.py中直接下載並安裝它的,因爲pip引發了一個錯誤。

pysmb軟件包的用戶界面不太友好,但對Windows客戶端來說它確實很好。

我成立了一個名爲「my_share」的Linux機器上的用戶共享「愛德華茲」使用smb.conf中輸入以下內容:

[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機器它的主機命令的輸出)

希望這可能會有用。

+0

我切換到pysmb並得到的東西馬上工作。謝謝你的幫助。 – grantathon

相關問題