帮助文档
专业提供香港服务器、香港云服务器、香港高防服务器租用、香港云主机、台湾服务器、美国服务器、美国云服务器vps租用、韩国高防服务器租用、新加坡服务器、日本服务器租用 一站式全球网络解决方案提供商!专业运营维护IDC数据中心,提供高质量的服务器托管,服务器机房租用,服务器机柜租用,IDC机房机柜租用等服务,稳定、安全、高性能的云端计算服务,实时满足您的多样性业务需求。 香港大带宽稳定可靠,高级工程师提供基于服务器硬件、操作系统、网络、应用环境、安全的免费技术支持。
服务器资讯 / 香港服务器租用 / 香港VPS租用 / 香港云服务器 / 美国服务器租用 / 台湾服务器租用 / 日本服务器租用 / 官方公告 / 帮助文档
python 访问共享文件夹 pysmb 带用户名密码 带密码共享 创建多级目录 复制本地文件夹到服务器
发布时间:2024-03-06 20:43:10   分类:帮助文档
python 访问共享文件夹 pysmb 带用户名密码 带密码共享 创建多级目录 复制本地文件夹到服务器




复制本地文件夹到服务器
def create_nested_directories(conn, share_name, path):
# 如果没有就创建多级目录
try:
conn.listPath(share_name, path)
return
except:
pass
directories = path.split('/')
current_path = ''
for directory in directories:
if directory:
current_path += '/' + directory
try:
conn.listPath(share_path, current_path)
except:
conn.createDirectory(share_name, current_path)


def store_remote_file(local_file_path, remote_dir, share_path='xtjfs01', host=host):
remote_file_path = os.path.join(remote_dir, os.path.basename(local_file_path))
with SMBConnection(login_dict) as conn:
result = conn.connect(host, 445) # smb协议默认端口445
print("登录状态", result)
# 如果没有目录就创建一个
# try:
# conn.listPath(share_path, remote_file_dir)
# except:
# conn.createDirectory(share_path, remote_file_dir)

create_nested_directories(conn,share_name=share_path,path=remote_dir)

try:
with open(local_file_path, 'rb') as f:
print(2323)
conn.storeFile(share_path, remote_file_path, f)
print('write remote file success')
except Exception as e:
print(e)
print('write remote file fail')



remote_dir = r'EP\{}'.format('2021')

# store_remote_file(local_file, remote_dir)

def copy_folder_to_server(source_folder, destination_folder, share_path='xtjfs01', host=host):
with SMBConnection(login_dict) as conn:
result = conn.connect(host, 445) # smb协议默认端口445
print("登录状态", result)
# 创建目标文件夹
# conn.mkdir(share_name, destination_folder)

# 遍历源文件夹中的文件和子文件夹
for item in os.listdir(source_folder):
source_item = os.path.join(source_folder, item)
destination_item = os.path.join(destination_folder, item)

# 如果是文件夹,则递归复制
if os.path.isdir(source_item):
copy_folder_to_server(source_item, destination_item)
# 如果是文件,则复制到服务器
elif os.path.isfile(source_item):
store_remote_file(source_item, destination_folder)
# with open(source_item, 'rb') as file:
# conn.storeFile(share_path, destination_item, file)


source_folder = r"E:\file\報告"
destination_folder = remote_dir
copy_folder_to_server(source_folder, destination_folder)

如何根据 ip 获取计算机名 ping -a 192.168.1.100
目标主机的 ip 地址 或域名,及下面的信息都不能错 my_name = “” # 这个随便,可以为空字符串
创建多级目录
创建前先判断,如果存在目录则不创建

def get_remote_file(local_file_path, remote_file_path):
with SMBConnection(login_dict) as conn:
result = conn.connect(host, 445) # smb协议默认端口445
with open(local_file_path, 'wb') as fw:
conn.retrieveFile(share_path,remote_file_path,fw)
print('local file path', local_file_path)
print("get remote file success")

def create_nested_directories(conn, share_name, path):
# 如果没有就创建多级目录
try:
conn.listPath(share_name, path)
return
except:
pass
# 如果这样不行,则将 / 改为 \\
directories = path.split('/')
current_path = ''
for directory in directories:
if directory:
current_path += '/' + directory
try:
conn.listPath(share_path, current_path)
except:
conn.createDirectory(share_name, current_path)


def store_remote_file(local_file_path, remote_file_path):
remote_file_dir = os.path.dirname(remote_file_path)
with SMBConnection(login_dict) as conn:
result = conn.connect(host, 445) # smb协议默认端口445
print("登录状态", result)
# 如果没有目录就创建一个
# try:
# conn.listPath(share_path, remote_file_dir)
# except:
# conn.createDirectory(share_path, remote_file_dir)

create_nested_directories(conn,share_name=share_path,path=remote_file_dir)

try:
with open(local_file_path, 'rb') as f:
print(2323)
conn.storeFile(share_path, remote_file_path, f)
print('write remote file success')
except Exception as e:
print(e)
print('write remote file fail')

login_dict = {
'username': username,
# 'password': password,
'password': password,
'my_name': my_name,
'remote_name': remote_name,
'is_direct_tcp': True,
'use_ntlm_v2': True
}

遍历可访问的共享目录
from smb import SMBConnection

try:
conn = SMBConnection(username,password,'name',system_name,domain,use_ntlm_v2=True,
sign_options=SMBConnection.SIGN_WHEN_SUPPORTED,
is_direct_tcp=True)
connected = conn.connect(system_name,445)

try:
Response = conn.listShares(timeout=30) # obtain a list of shares
print('Shares on: ' + system_name)

for i in range(len(Response)): # iterate through the list of shares
print(" Share[",i,"] =", Response[i].name)

try:
# list the files on each share
Response2 = conn.listPath(Response[i].name,'/',timeout=30)
print(' Files on: ' + system_name + '/' + " Share[",i,"] =",
Response[i].name)
for i in range(len(Response2)):
print(" File[",i,"] =", Response2[i].filename)
except:
print('# can not access the resource')
except:
print('# can not list shares')
except:
print('# can not access the system')

listpath 列出子目录方式
conn.listPath(share_path, remote_file_dir)
conn.listPath('root', 'sub\sub_second\sub_third')

with 方式连接
try:
# with方式,使用后自动close连接
with SMBConnection(username, password, "", "", use_ntlm_v2=True) as conn:
result = conn.connect(host, 445) # smb协议默认端口445






连接服务器

```python
from smb.SMBConnection import SMBConnection

host = "192.168.1.2" #ip或域名,改成你自己的
username = "user" #用户名,改成你自己的
password = "pass" #密码,改成你自己的
my_name = "aaaa" # 这个随便,可以为空字符串
remote_name = "WIN-1QI0CPE887P" # 这个是共享主机的主机名,listShares会用到,不用listShares的话可以为空字符串
conn = SMBConnection(username, password,my_name , remote_name , is_direct_tcp=True)
result = conn.connect(host, 445) #smb协议默认端口445
print("登录状态", result)


上传文件
with open('1.txt', 'rb') as f:
conn.storeFile("Software","11.txt", f)

下载文件
with open('1.txt', 'wb') as fw:
conn.retrieveFile("Software","11.txt",fw)

删除文件 第二个参数是要删除的文件,可包含通配符。第三个参数是否删除目录 `conn.deleteFiles(“Software”, “test/*”, delete_matching_folders=True)
创建文件夹
创建文件夹
无返回值,没报错就是创建成功
conn.createDirectory("Software", "test")

重命名文件
重命名文件
conn.rename("Software","11.txt","22.txt")

参考 https://blog..net/Qwertyuiop2016/article/details/129367401 https://blog..net/firseve/article/details/119635385 https://blog..net/qqyuanhao163/article/details/100145749




香港云服务器租用推荐
服务器租用资讯
·广东云服务有限公司怎么样
·广东云服务器怎么样
·广东锐讯网络有限公司怎么样
·广东佛山的蜗牛怎么那么大
·广东单位电话主机号怎么填写
·管家婆 花生壳怎么用
·官网域名过期要怎么办
·官网邮箱一般怎么命名
·官网网站被篡改怎么办
服务器租用推荐
·美国服务器租用
·台湾服务器租用
·香港云服务器租用
·香港裸金属服务器
·香港高防服务器租用
·香港服务器租用特价