找不到网络名称-在Windows中使用Python复制文件

路易斯·汉里克

我试图将文件从服务器远程复制到执行脚本的位置,但是它使我返回连接错误,如果我通过Winodws上的RDP建立连接,则可以正常连接到主机

点击查看源代码

script.py

#!/usr/bin/env python
#win32wnetfile.py

import os
import os.path
import shutil
import sys
import win32wnet

def netcopy(host, source, dest_dir, username=None, password=None, move=False):
    """ Copies files or directories to a remote computer. """

    wnet_connect(host, username, password)

    dest_dir = covert_unc(host, dest_dir)

    # Pad a backslash to the destination directory if not provided.
    if not dest_dir[len(dest_dir) - 1] == '\\':
        dest_dir = ''.join([dest_dir, '\\'])

    # Create the destination dir if its not there.
    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)
    else:
        # Create a directory anyway if file exists so as to raise an error.
         if not os.path.isdir(dest_dir):
             os.makedirs(dest_dir)

    if move:
        shutil.move(source, dest_dir)
    else:
        shutil.copy(source, dest_dir)

def covert_unc(host, path):
    """ Convert a file path on a host to a UNC path."""
    return ''.join(['\\\\', host, '\\', path.replace(':', '$')])

def wnet_connect(host, username, password):
    unc = ''.join(['\\\\', host])
    try:
        win32wnet.WNetAddConnection2(0, None, unc, None, username, password)
    except Exception, err:
        if isinstance(err, win32wnet.error):
            # Disconnect previous connections if detected, and reconnect.
            if err[0] == 1219:
                win32wnet.WNetCancelConnection2(unc, 0, 0)
                return wnet_connect(host, username, password)
        raise err

if __name__ == '__main__':

    netcopy('192.168.9.254', 'C:\\Program Files (x86)\\Data\\connect.cfg', 'c:\\', 'localdomain\Administrator', 'pw1234')

输出量

  File "script.py", line 13, in netcopy
    wnet_connect(host, username, password)
  File "script.py", line 67, in wnet_connect
    raise err
pywintypes.error: (67, 'WNetAddConnection2', 'The network name cannot be found.')
克里斯蒂法蒂

根据[MS.Docs]:WNetAddConnection2W函数重点是我的):

  • lpRemoteName

    指向以字符结尾的字符串的指针,该字符串指定要连接的网络资源。该字符串的长度最多为MAX_PATH个字符,并且必须遵循网络提供商的命名约定

因此,它应该是一个共享名。我修改了代码工作在我的环境(只有一个变化-更换netcopy从电话netcopy('192.168.9.254', 'C:\\Program Files (x86)\\Data\\connect.cfg', 'c:\\', 'localdomain\Administrator', 'pw1234')netcopy("127.0.0.1", "C:\\c\\a.txt", "C$"),因为我连接到本地主机,我有一些股份)。
注意dest_dir值:C $

输出

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q061107274]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> net share

Share name   Resource                        Remark

-------------------------------------------------------------------------------
ADMIN$       C:\WINDOWS                      Remote Admin
C$           C:\                             Default share
E$           E:\                             Default share
F$           F:\                             Default share
G$           G:\                             Default share
IPC$                                         Remote IPC
L$           L:\                             Default share
M$           M:\                             Default share
N$           N:\                             Default share
share-cfati  L:\Share\cfati
share-public L:\Share\public
The command completed successfully.


[prompt]> dir /b c:\a*
File Not Found

[prompt]> "e:\Work\Dev\VEnvs\py_pc064_02.07.17_test0\Scripts\python.exe" code_orig.py

[prompt]> dir /b c:\a*
a.txt

作为一个侧面说明,在远程机器上的用户必须具有管理权限,否则写文件可能会失败与ERROR_ACCESS_DENIED0x00000005),特别是因为你的目标C: 它对我有用,因为我的用户在我的计算机上拥有“上帝一样”的特权。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Windows之后找不到任何网络

文件名称怪异,复制后找不到

python re找不到此分组名称

使用 MobX 在 Typescript 中找不到名称

使用python的网页抓取找不到网络链接

在“ System.Windows.Style”的名称范围中找不到名称

在 angular 中使用声明的模块时找不到名称

尝试访问文件共享时出现“找不到网络路径”

使用 rtl8723be 找不到 ubuntu 18.04 的 Wifi 网络

XPATH 找不到网络上包含的文本 - 使用 contains(text())

使用puppeteer进行网络抓取找不到CSS标签

Windows 8-WiFi适配器找不到无线网络!最终以“找不到网络”结束

pyspark:在jar文件中找不到名称空间

找不到所需的文件。名称:index.html

在Angular 4 TypeScript中的JavaScript文件中找不到名称

WebStorm:在某些.ts文件中找不到名称“ await”

添加test.ts文件时找不到名称“ XLSX”

Tox“错误:找不到文件:<项目名称>”

grep 在文本文件中找不到名称

如果其路径/名称包含空格,则系统找不到文件

C ++ / CX标头文件找不到Microsoft名称空间

在Windows Phone上找不到“按钮”的名称图像的属性

在Windows Phone 8中找不到类型或名称空间pushPin

错误无法复制文件“ obj \ Debug \ [项目名称] .dll”,因为找不到该文件

致命错误:使用名称空间时找不到类

网络核心:类型或名称空间名称“ RoleProvider”找不到

网络核心:找不到类型或名称空间名称“ GlobalFilterCollection”

找不到文件:在我的网络上使用Mac运行Xamarin App时,Contents.json错误

C#在整个网络位置使用File.Move()找不到文件