no such file:没有这样的文件。产生的原因:文件路径出错,文件不存在或者文件名没有加上后缀。问题解决:在我确认我的路径正确后发现,在用ssh协议进行文件传输时,只能将tmp目录下的文件进行传输。所以我将文件转移到tmp下,然后成功了。
以下是部分代码
class UpLoad(object):
private_key_path = 'the path where your private key locates in'
key = rsakey.RSAKey.from_private_key_file(private_key_path)
print(key)
def __init__(self, host='your host IP', port=22, username='your username', key=key):
self.host = host
self.port = port
self.username = username
self.key = key
self.__k = None
def run(self):
self.connect()
pass
self.close()
def connect(self):
transport = trans.Transport((self.host, self.port))
transport.connect(username=self.username,pkey=self.key)
self.__transport = transport
def close(self):
self.__transport.close()
def upload(self, local_path, target_path):
# make a connection
sftp = Sftp.SFTPClient.from_transport(self.__transport)
# download a file from the server in virtual device
sftp.put(local_path, target_path)
print(target_path[5:], "Successfully upload!")
def main():
ssh = UpLoad()
ssh.connect()
ssh.upload('/tmp/filename.py', "/tmp/filename.py")
ssh.close()
if __name__ == '__main__':
main()