The SMBHandler class provides support for “smb://” URLs in the urllib2 python package.
The following code snippet illustrates file retrieval.:
# -*- coding: utf-8 -*-
import urllib2
from smb.SMBHandler import SMBHandler
director = urllib2.build_opener(SMBHandler)
fh = director.open('smb://myuserID:mypassword@192.168.1.1/sharedfolder/rfc1001.txt')
# Process fh like a file-like object and then close it.
fh.close()
# For paths/files with unicode characters, simply pass in the URL as an unicode string
fh2 = director.open(u'smb://myuserID:mypassword@192.168.1.1/sharedfolder/测试文件夹/垃圾文件.dat')
# Process fh2 like a file-like object and then close it.
fh2.close()
The following code snippet illustrates file upload. You need to provide a file-like object for the data parameter in the open() method:
import urllib2
from smb.SMBHandler import SMBHandler
file_fh = open('local_file.dat', 'rb')
director = urllib2.build_opener(SMBHandler)
fh = director.open('smb://myuserID:mypassword@192.168.1.1/sharedfolder/upload_file.dat', data = file_fh)
# Reading from fh will only return an empty string
fh.close()