2015-12-02 104 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

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