服务器上的 smb 协议,使用smb协议python3访问服务器上的远程文件

我有一些文件的远程服务器。smb://ftpsrv/public/

我可以在那里被授权为匿名用户。在java中,我可以简单地编写这样的代码

SmbFile root = new SmbFile(SMB_ROOT);

并且能够处理里面的文件(这是我所需要的,一行!),但我在python3中找不到如何处理此任务的方法,有很多资源,但我认为它们与我无关,因为它们经常为python2和旧方法量身定制。有没有类似于上面java代码的简单方法?

或者,如果我想访问smb://ftpsrv/public/文件夹中的文件fgg.txt,是否可以提供真正的工作解决方案。有没有真正方便的lib来解决这个问题?

例如现场import tempfile

from smb.SMBConnection import SMBConnection

# There will be some mechanism to capture userID, password, client_machine_name, server_name and server_ip

# client_machine_name can be an arbitary ASCII string

# server_name should match the remote machine name, or else the connection will be rejected

conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2 = True)

assert conn.connect(server_ip, 139)

file_obj = tempfile.NamedTemporaryFile()

file_attributes, filesize = conn.retrieveFile('smbtest', '/rfc1001.txt', file_obj)

# Retrieved file contents are inside file_obj

# Do what you need with the file_obj and then close it

# Note that the file obj is positioned at the end-of-file,

# so you might need to perform a file_obj.seek() if you need

# to read from the beginning

file_obj.close()

我真的需要提供所有这些细节吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值