Paramiko SFTP - 避免指定完整的本地文件名?

发布于 2024-12-01 05:48:30 字数 1422 浏览 0 评论 0原文

我有一些使用 Paramiko 从远程服务器获取构建文件的 Python 代码:

def setup_sftp_session(self, host='server.com', port=22, username='puppy'):
    self.transport = paramiko.Transport((host, port))
    privatekeyfile = os.path.expanduser('~/.ssh/id_dsa')
    try:
        ssh_key = paramiko.DSSKey.from_private_key_file(privatekeyfile)
    except IOError, e:
        self.logger.error('Unable to find SSH keyfile: %s' % str(e))
        sys.exit(1)
    try:
        self.transport.connect(username = username, pkey = ssh_key)
    except paramiko.AuthenticationException, e:
        self.logger.error("Unable to logon - are you sure you've added the pubkey to the server?: %s" % str(e))
        sys.exit(1)
    self.sftp = paramiko.SFTPClient.from_transport(self.transport)
    self.sftp.chdir('/some/location/buildfiles')

def get_file(self, remote_filename):
    try:
        self.sftp.get(remote_filename, 'I just want to save it in the local cwd')
    except IOError, e:
        self.logger.error('Unable to find copy remote file %s' % str(e))

def close_sftp_session(self):
    self.sftp.close()
    self.transport.close()

我想检索每个文件,并将其存放在当前的本地工作目录中。

然而,Paramiko 似乎没有这个选项 - 您需要指定完整的本地目的地。您甚至无法指定目录(例如“./”,甚至“/home/victorhooi/files”) - 您需要包括文件名的完整路径。

有什么办法解决这个问题吗?如果我们还必须指定本地文件名,而不是仅仅复制远程文件名,那会很烦人。

另外 - 我在 setup_sftp_session 中使用 exit(1) 处理异常的方式 - 这是一个很好的做法,还是有更好的方法?

干杯, 胜利者

I have some Python code that uses Paramiko to grab build files from a remote server:

def setup_sftp_session(self, host='server.com', port=22, username='puppy'):
    self.transport = paramiko.Transport((host, port))
    privatekeyfile = os.path.expanduser('~/.ssh/id_dsa')
    try:
        ssh_key = paramiko.DSSKey.from_private_key_file(privatekeyfile)
    except IOError, e:
        self.logger.error('Unable to find SSH keyfile: %s' % str(e))
        sys.exit(1)
    try:
        self.transport.connect(username = username, pkey = ssh_key)
    except paramiko.AuthenticationException, e:
        self.logger.error("Unable to logon - are you sure you've added the pubkey to the server?: %s" % str(e))
        sys.exit(1)
    self.sftp = paramiko.SFTPClient.from_transport(self.transport)
    self.sftp.chdir('/some/location/buildfiles')

def get_file(self, remote_filename):
    try:
        self.sftp.get(remote_filename, 'I just want to save it in the local cwd')
    except IOError, e:
        self.logger.error('Unable to find copy remote file %s' % str(e))

def close_sftp_session(self):
    self.sftp.close()
    self.transport.close()

I'd like to retrieve each file, and deposit it in the current local working directory.

However, Paramiko doesn't seem to have an option for this - you need to specify the full local destination. You can't even specify a directory (e.g. "./", or even "/home/victorhooi/files") - you need the full path including filename.

Is there any way around this? It'll be annoying if we have to specify the local filename as well, instead of just copying the remote one.

Also - the way I'm handling exceptions in setup_sftp_session, with exit(1) - is that a good practice, or is there a better way?

Cheers,
Victor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-12-08 05:48:30

你必须

os.path.join(os.getcwd(), remote_filename)

在函数中插入调用 exit() 不是一个好主意。也许您想重用代码并在出现异常时采取一些操作。如果你继续调用 exit() ,你就会迷路。
我建议修改此函数,以便在成功时返回 True,否则返回 False。然后调用者可以决定要做什么。

另一种方法是不捕获异常。因此调用者必须处理它们,并且调用者获得有关失败情况的完整信息(包括堆栈跟踪)。

you have to insert

os.path.join(os.getcwd(), remote_filename)

Calling exit() in a function is not a good idea. Maybe you want to reuse the code and take some action in case of an exception. If you keep the exit() call you are lost.
I suggest to modify this function such that a True is returned in case of success, and False else. Then the caller can decide what to do.

Another approach would be not to catch the exceptions. So the caller has to handle them and the caller gets the full information (including the stacktrace) about the circumstances of the failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文