Using a trailing forward slash in Windows causes problems
greenfoo opened this issue · comments
Short description
If I copy "src" to "dst", it works.
If I copy "src/" to "dst", it fails (only when running on Windows).
More details
Let' s see it with an example.
First of all, in Linux, let's create a test folder structure:
$ mkdir data
$ touch data/1.txt
$ touch data/2.txt
$ mkdir data/3
$ touch data/3/3.txt
$ tree
.
└── data
├── 1.txt
├── 2.txt
└── 3
└── 3.txt
Next, let's create an example (test.py
) that makes use of the scp library:
import paramiko
import os
import sys
import scp
c=paramiko.SSHClient()
c.load_system_host_keys(filename=os.devnull)
c.load_host_keys(filename=os.devnull)
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("example.com", username="joe", password="doe")
s=scp.SCPClient(c.get_transport(), sanitize=lambda x: x, socket_timeout=60)
s.put(sys.argv[1], "/tmp", recursive=True)
Finally, let's use this script to copy the folder data to a remote server:
$ test.py data
$ ssh example.com tree /tmp
/tmp
└── data
├── 1.txt
├── 2.txt
└── 3
└── 3.txt
We can also run the same script with "data/" instead of "data" if we want to transfer the contents of the folder and not the folder itself:
$ test.py data/
$ ssh example.com tree /tmp
/tmp
├── 1.txt
├── 2.txt
└── 3
└── 3.txt
So far, so good. The problem comes when we try the same thing in Windows: the first example (data) works, but the second example (data/) results in this error:
c:\work>python test.py data/
Traceback (most recent call last):
File "c:\work\test.py", line 13, in <module>
c2.put(sys.argv[1], "/tmp", recursive=True)
File "C:\Program Files\Python39\lib\site-packages\scp.py", line 203, in put
self._send_recursive(files)
File "C:\Program Files\Python39\lib\site-packages\scp.py", line 373, in _send_recursive
self._chdir(last_dir, asbytes(root))
File "C:\Program Files\Python39\lib\site-packages\scp.py", line 361, in _chdir
self._send_pushd(to_dir)
File "C:\Program Files\Python39\lib\site-packages\scp.py", line 387, in _send_pushd
self._recv_confirm()
File "C:\Program Files\Python39\lib\site-packages\scp.py", line 415, in _recv_confirm
raise SCPException('No response from server')
scp.SCPException: No response from server