Python - 处理 DMZ 时出现 FileNotFoundError

Python - FileNotFoundError when dealing with DMZ

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

但是,当我尝试将源更改为安装在 DMZ 中的服务器中的路径并将目标更改为本地服务器中的文件夹时,我收到以下错误:

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

这是脚本:

import sys, os, shutil
import glob
import os.path, time

fob= open(r"C:\Log.txt","a")
dir_src = r"\reports"
dir_dst = r"C:\Dest"
dir_bkp = r"C:\Bkp"

for w in list(set(os.listdir(dir_src)) - set(os.listdir(dir_bkp))):
    if w.endswith('.nessus'):
        pathname = os.path.join(dir_src, w)
        Date_File="%s" %time.ctime(os.path.getmtime(pathname))
        print (Date_File)
        if os.path.isfile(pathname):
                        shutil.copy2(pathname, dir_dst)
                        shutil.copy2(pathname, dir_bkp)
                        fob.write("File Name:   %s" % os.path.basename(pathname))
                        fob.write("   Last modified Date:   %s" % time.ctime(os.path.getmtime(pathname)))
                        fob.write("   Copied On:   %s" % time.strftime("%c"))
                        fob.write("\n")

fob.close()
os.system("PAUSE")

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

  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</p>

<pre><code>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. 如果您的远程文件夹还在,请说明。我还没有找到一个对所有人一视同仁的解决方案,但我对其中一个很感兴趣。