我有一些文件的远程服务器。
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()
我真的需要提供所有这些细节吗?
Python 3中使用urllib和pysmb打开文件的简单示例
我还没有准备好使用匿名SMB共享进行测试,但此代码应该可以工作。
urllib2是python 2包,在python 3中它被重命名为urllib,一些东西被移动了。
相关问题 更多 >
编程相关推荐