2015-12-02 105 views
1

我有一個Odoo8在我的Linux服務器上運行,我需要從這臺服務器上的文件複製到Windows 10共享文件夾與身份驗證。 我試圖做到這一點編程這樣的:通過網絡在odoo網絡寫身份驗證

full_path = "smb://hostname/shared_folder/other_path" 
if not os.path.exists(full_path): 
    os.makedirs(full_path) 
full_path = os.path.join(full_path, file_name) 
bin_value = stream.decode('base64') 
if not os.path.exists(full_path): 
    try: 
     with open(full_path, 'wb') as fp: 
      fp.write(bin_value) 
      fp.close() 
     return True 
    except IOError: 
     _logger.exception("stream_save writing %s", full_path) 

,但即使不發生異常,不創建文件夾和文件不被寫入。 然後我試圖從uri中刪除「smb:」部分,它引發了關於認證的異常。

我想通過使用python解決這個問題,可能會避免os.system調用或外部腳本,但如果沒有其他方式可行,那麼任何建議是值得歡迎的。

我也試圖與

"//user:[email protected]" 

"//domain;user:[email protected]" 

既沒有SMB

回答

1

嗯,我發現了這件事由我自己使用SAMBA方式:

第一你需要安裝pysmb(pip install pysmb) N:

from smb.SMBConnection import SMBConnection 
conn = SMBConnection(user, password, "my_name", server, domain=domain, use_ntlm_v2 = True) 
conn.connect(ip_server) 
conn.createDirectory(shared_folder, sub_directory) 
file_obj = open(local_path_file,'rb') 
conn.storeFile(shared_folder, sub_directory+"/"+filename, file_obj) 
file_obj.close() 

在我的情況sub_directory是整個路徑,因此我需要創建每個文件夾逐個(createDirectory只能這樣),每次我需要檢查,如果該目錄不已經存在,因爲否則createDirectory引發異常。

我希望我的解決方案對其他人有用。

如果有人找到更好的解決方案,請回答...

+0

這可能是有用的.. !! –