You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pysmb从Windows共享驱动器下载文件如何提速?

优化SMB文件下载速度的方案

一、针对pysmb的参数与逻辑优化

1. 增大传输缓冲区+指定SMB3协议

pysmb默认缓冲区较小,频繁IO交互会拖慢速度,同时旧版SMB协议效率偏低。手动设置大缓冲区并指定SMB3协议:

import pysmb

conn = SMBConnection(username, password, client_machine_name, server_name, 
use_ntlm_v2=True, smb_version=3)  # 优先用SMB3提升传输效率
conn.connect(host, 139)
buffer_size = 1024 * 1024  # 1MB缓冲区,减少读写次数
with open("local_file.txt", "wb") as local_file:
     conn.retrieveFile(share, file_path, local_file, buffer_size=buffer_size)

2. 多线程分段下载

利用retrieveFile的start/end参数实现并行分段下载,充分利用带宽:

import pysmb
import threading
import os

def download_segment(conn, share, remote_path, local_path, start, end):
    with open(local_path, "rb+") as f:
        f.seek(start)
        conn.retrieveFile(share, remote_path, f, start=start, end=end)

# 初始化连接
conn = SMBConnection(username, password, client_machine_name, server_name, 
use_ntlm_v2=True, smb_version=3)
conn.connect(host, 139)

# 获取文件大小
file_info = conn.getAttributes(share, file_path)
file_size = file_info.file_size

# 分4段下载(可根据网络调整段数)
segment_count = 4
segment_size = file_size // segment_count
threads = []

# 预分配本地文件空间
with open("local_file.txt", "wb") as f:
    f.truncate(file_size)

for i in range(segment_count):
    start = i * segment_size
    end = start + segment_size - 1 if i != segment_count-1 else file_size - 1
    t = threading.Thread(target=download_segment, args=(conn, share, file_path, "local_file.txt", start, end))
    threads.append(t)
    t.start()

for t in threads:
    t.join()

二、替代方案(性能更优)

1. 使用smbclient库

基于libsmbclient开发,性能优于纯Python实现的pysmb:

from smbclient import open_file

with open_file(f"//{server_name}/{share}/{file_path}", mode='rb', username=username, password=password) as remote_file:
    with open("local_file.txt", "wb") as local_file:
        # 分块读取避免内存占用过高
        while chunk := remote_file.read(1024*1024):
            local_file.write(chunk)

2. 调用系统原生工具

系统自带SMB工具经过深度优化,速度远快于Python库,用subprocess调用:

  • Windows环境:
import subprocess

subprocess.run(f'copy \\\\{host}\\{share}\\{file_path} local_file.txt', shell=True, check=True)
  • Linux/macOS环境:
import subprocess

# 用smbget直接下载
subprocess.run(f'smbget smb://{username}:{password}@{host}/{share}/{file_path} -o local_file.txt', shell=True, check=True)

3. Windows专属:pywin32库

利用Windows原生API操作SMB,性能拉满:

import win32file

remote_path = f"\\\\{host}\\{share}\\{file_path}"
local_path = "local_file.txt"
win32file.CopyFile(remote_path, local_path, False)

内容的提问来源于stack exchange,提问作者Gary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 00:52:42