Я пытаюсь скопировать файл с сервера удаленно туда, где выполняется скрипт, однако он возвращает мне ошибку в соединении, если я устанавливаю соединение через RDP от winodws, я подключаюсь нормально к хосту
Нажмите, чтобы посмотреть исходный код
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.')