Python 在脚本执行期间重新加载变量

Python 在脚本执行期间重新加载变量,python,authentication,sftp,.netrc,Python,Authentication,Sftp,.netrc,我有一个脚本,我正在使用它连接到sftp服务器并下载一些文件,关键是我需要为多个用户总共7个这样做。我已经让这个工作,但我基本上重复自己7次,让这个工作,我只是把三个在下面的例子。我要做的是让脚本在每个会话关闭后重新检查Hostname变量。有没有一种方法可以做到这一点而不必每次都覆盖它 以下是我到目前为止的情况: import pysftp ` import netrc ` """ This imports from three different directories which are

我有一个脚本,我正在使用它连接到sftp服务器并下载一些文件,关键是我需要为多个用户总共7个这样做。我已经让这个工作,但我基本上重复自己7次,让这个工作,我只是把三个在下面的例子。我要做的是让脚本在每个会话关闭后重新检查Hostname变量。有没有一种方法可以做到这一点而不必每次都覆盖它

以下是我到目前为止的情况:

import pysftp
`
import netrc
`
"""
This imports from three different directories which are owned by different users.
it logs in as a user, downloads their files, closes the connection and then opens another with the other user.
it uses login details from the .netrc files to pull the usernames.
it is a bit clunky repeating all this code three times, there must be a better way but it works.
"""

#I am using netrc to conseal the login details from easy access
secrets = netrc.netrc()
Host = "ubuntu-test"

user = "user1"
username, account, password = secrets.authenticators(user)
sftp = pysftp.Connection(Host,username=username, password=password)
sftp.get_d("/foo", "/home/Documents", preserve_mtime=True)
sftp.close

user = "user2"
#If I take out the next line it still uses the user details from the previous section.
username, account, password = secrets.authenticators(user)
sftp = pysftp.Connection(Host, username=username, password=password)
sftp.get_d("/foo", "/home/Documents", preserve_mtime=True)
sftp.close()

user = "user3"
username, account, password = secrets.authenticators(user)
sftp = pysftp.Connection(Host, username=username, password=password)
sftp.get_d("/foo", "/home/Documents", preserve_mtime=True)
sftp.close()
*编辑1


根据以下建议,我将其重新格式化为函数,这正是我想要的,所以现在它是:

def download(login, file):
    username, account, password = secrets.authenticators(login)
    sftp = pysftp.Connection(server, username=username, password=password)
    sftp.get_d(file, destination, preserve_mtime=True)
    sftp.close()

download("<login>", "/foo")
def download(login, file):
    username, account, password = secrets.authenticators(login)
    sftp = pysftp.Connection(server, username=username, password=password)
    sftp.get_d(file, destination, preserve_mtime=True)
    sftp.close()

download("<login1>", "/foo")
download("<login2>", "/foo")
download("<login3>", "/foo")

根据以下建议,我将其重新格式化为函数,这正是我想要的,所以现在它是:

def download(login, file):
    username, account, password = secrets.authenticators(login)
    sftp = pysftp.Connection(server, username=username, password=password)
    sftp.get_d(file, destination, preserve_mtime=True)
    sftp.close()

download("<login>", "/foo")
def download(login, file):
    username, account, password = secrets.authenticators(login)
    sftp = pysftp.Connection(server, username=username, password=password)
    sftp.get_d(file, destination, preserve_mtime=True)
    sftp.close()

download("<login1>", "/foo")
download("<login2>", "/foo")
download("<login3>", "/foo")

为什么不能将其移动到以用户为参数的函数中?我可能不明白有没有一种方法可以做到这一点而不必每次都重写它?写一个函数。