在脚本期间重新加载变量
re-load variables during script
我有一个脚本,我用它来连接到 sftp 服务器并下载一些文件,问题是我需要为多个用户(总共 7 个)执行此操作。我已经完成了这个工作,但我基本上重复了 7 次才能让它工作(我在下面的示例中只放了三个)。我想要做的是让脚本在每次会话关闭后重新检查主机名变量。有没有办法不用每次都覆盖它?
这是我目前的情况:
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")
我有一个脚本,我用它来连接到 sftp 服务器并下载一些文件,问题是我需要为多个用户(总共 7 个)执行此操作。我已经完成了这个工作,但我基本上重复了 7 次才能让它工作(我在下面的示例中只放了三个)。我想要做的是让脚本在每次会话关闭后重新检查主机名变量。有没有办法不用每次都覆盖它?
这是我目前的情况:
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")