通过身份验证在odoo中通过网络写入文件

Write a file over network in odoo with authentication

我的 linux 服务器上有一个 Odoo8 运行,我需要将文件从该服务器复制到 Windows 10 共享文件夹并进行身份验证。 我试着像这样以编程方式做到这一点:

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:password@hostname"

"//domain;user:password@hostname"

有和没有smb

嗯,我自己用SAMBA找到的方法:

首先你需要安装pysmb (pip install pysmb) 然后:

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 会引发异常。

我希望我的解决方案对其他人有用。

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