处理DMZ时Python FileNotFoundError

2024-06-08 14:50:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个python脚本来将文件从源文件夹复制到目标文件夹,该脚本在本地计算机上运行良好。在

但是,当我试图将源更改为位于DMZ中安装的服务器中的路径,并将目标更改为本地服务器中的文件夹时,出现以下错误:

FileNotFoundError: [WinError 3] The system cannot find the path specified: '\reports'

下面是脚本:

^{pr2}$

Tags: 文件the路径服务器脚本文件夹目标计算机
1条回答
网友
1楼 · 发布于 2024-06-08 14:50:42

好的,我们首先要看看你有什么样的远程文件夹。在

  1. 如果您的远程文件夹是共享的windows网络文件夹,请尝试将其映射为网络驱动器:http://windows.microsoft.com/en-us/windows/create-shortcut-map-network-drive#1TC=windows-7 然后您就可以使用Z:\reports之类的东西来访问文件。

  2. 如果您的远程文件夹实际上是unix服务器,则可以使用paramiko访问它并从中复制文件:


import paramiko, sys, os, posixpath, re

def copyFilesFromServer(server, user, password, remotedir, localdir, filenameRegex = '*', autoTrust=True):
    # Setup ssh connection for checking directory
    sshClient = paramiko.SSHClient()
    if autoTrust:
        sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #No trust issues! (yes this could potentially be abused by someone malicious with access to the internal network)
    sshClient.connect(server,user,password)
    # Setup sftp connection for copying files
    t = paramiko.Transport((server, 22))
    t.connect(user, password)
    sftpClient = paramiko.SFTPClient.from_transport(t)
    fileList = executeCommand(sshclient,'cd {0}; ls | grep {1}'.format(remotedir, filenameRegex)).split('\n')
    #TODO: filter out empties!
    for filename in fileList:
        try:
            sftpClient.get(posixpath.join(remotedir, filename), os.path.join(localdir, filename), callback=None) #callback for showing number of bytes transferred so far
        except IOError as e:
            print 'Failed to download file <{0}> from <{1}> to <{2}>'.format(filename, remotedir, localdir)

  1. 如果你的远程文件夹是用webdav协议提供的,我和你一样对答案感兴趣。

  2. 如果您的远程文件夹仍然是其他东西,请解释。我还没有找到一个平等对待所有人的解决办法,但我很感兴趣。

相关问题 更多 >