如何在共享网络驱动器上搭建可运行Python脚本的简易网页
无需复杂配置的共享驱动器脚本调用方案
核心逻辑说明
你之前用pushd运行脚本的本质是在本地电脑执行共享驱动器上的Python程序和脚本——共享驱动器的文件相当于映射到本地的资源,所有操作都在用户自己的电脑上完成,不需要搭建后端服务器。基于这个原理,我们可以用几种极简方式实现同事自主调用脚本的需求,完全不用碰复杂的网络配置。
方案1:HTML + VBScript(Windows环境下最便捷)
适合直接在浏览器操作,无需额外下载文件,利用Windows原生的VBScript触发本地命令执行:
- 在共享驱动器的可访问目录下创建
run_script.html文件,内容如下(替换实际路径):
<!DOCTYPE html> <html> <head> <title>文件复制工具</title> <style> .container { margin: 20px; } .input-group { margin: 10px 0; } button { padding: 8px 15px; cursor: pointer; } </style> </head> <body> <div class="container"> <h3>输入参数执行文件复制</h3> <div class="input-group"> <label>要复制的文件名(arg1): </label> <input type="text" id="arg1" placeholder="例如:data.csv"> </div> <div class="input-group"> <label>SFTP服务器地址(arg2): </label> <input type="text" id="arg2" placeholder="例如:sftp://myserver.com"> </div> <button onclick="runScript()">开始复制</button> <div id="result" style="margin-top:15px;color:green;"></div> </div> <script type="text/vbscript"> Sub runScript() Dim arg1, arg2, cmd, shell, exec arg1 = Document.getElementById("arg1").Value arg2 = Document.getElementById("arg2").Value If arg1 = "" Or arg2 = "" Then Document.getElementById("result").InnerHTML = "请填写所有参数!" Exit Sub End If ' 替换成你实际的共享路径和脚本路径 cmd = "cmd /c pushd ""\\myworkplace.com\mydept\...\python"" && python.exe scriptPath/script.py """ & arg1 & """ """ & arg2 & """" Set shell = CreateObject("WScript.Shell") Set exec = shell.Exec(cmd) Do While exec.Status = 0 WScript.Sleep 100 Loop If exec.ExitCode = 0 Then Document.getElementById("result").InnerHTML = "文件复制完成!" Else Document.getElementById("result").InnerHTML = "执行出错,请检查参数或联系管理员。" End If End Sub </script> </body> </html>
同事使用方式:
- 直接在浏览器地址栏输入共享路径(如
myworkplace.com/mydept/xxx/run_script.html) - 或在文件资源管理器找到该文件,双击打开
- 直接在浏览器地址栏输入共享路径(如
注意:
- Edge浏览器需开启"Internet Explorer模式下允许VBScript",或直接用IE打开
- 不需要同事本地装Python,共享驱动器上的
python.exe会直接运行
方案2:HTML + 批处理生成器(兼容所有浏览器)
如果不想依赖VBScript,可以让网页生成批处理文件,用户下载后双击运行:
创建generate_batch.html文件:
<!DOCTYPE html> <html> <head> <title>生成复制脚本</title> <style> .container { margin: 20px; } .input-group { margin: 10px 0; } button { padding: 8px 15px; cursor: pointer; } </style> </head> <body> <div class="container"> <h3>生成文件复制批处理</h3> <div class="input-group"> <label>要复制的文件名(arg1): </label> <input type="text" id="arg1" placeholder="例如:data.csv"> </div> <div class="input-group"> <label>SFTP服务器地址(arg2): </label> <input type="text" id="arg2" placeholder="例如:sftp://myserver.com"> </div> <button onclick="generateBatch()">生成并下载脚本</button> </div> <script> function generateBatch() { const arg1 = document.getElementById('arg1').value; const arg2 = document.getElementById('arg2').value; if (!arg1 || !arg2) { alert('请填写所有参数!'); return; } // 替换成实际路径 const batchContent = `@echo off pushd "\\myworkplace.com\mydept\...\python" python.exe scriptPath/script.py "${arg1}" "${arg2}" echo 执行完成,按任意键退出... pause`; const blob = new Blob([batchContent], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'copy_file.bat'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); } </script> </body> </html>
方案3:直接用共享驱动器上的批处理模板(最极简)
如果连网页都不想做,直接在共享驱动器放一个批处理模板:
copy_template.bat内容:
@echo off set /p arg1=请输入要复制的文件名: set /p arg2=请输入SFTP服务器地址: pushd "\\myworkplace.com\mydept\...\python" python.exe scriptPath/script.py "%arg1%" "%arg2%" echo 执行完成,按任意键退出... pause
同事双击这个文件,按提示输入参数即可,完全零配置。
通用注意事项
- 所有方案都是在用户本地电脑执行脚本,完全利用共享驱动器的现有权限,不会涉及服务器端配置
- 确保共享驱动器上的
python.exe、脚本文件对所有部门成员有读取和执行权限 - 如果脚本依赖第三方Python库,要把库打包到共享驱动器的Python环境中(比如用便携版Python)
内容的提问来源于stack exchange,提问作者Jesus Rodriguez
相关产品推荐
相关产品推荐

