本文整理汇总了Python中pysftp.Connection方法的典型用法代码示例。如果您正苦于以下问题:Python pysftp.Connection方法的具体用法?Python pysftp.Connection怎么用?Python pysftp.Connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pysftp
的用法示例。
在下文中一共展示了pysftp.Connection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_conn
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def get_conn(self) -> pysftp.Connection:
"""
Returns an SFTP connection object
"""
if self.conn is None:
cnopts = pysftp.CnOpts()
if self.no_host_key_check:
cnopts.hostkeys = None
cnopts.compression = self.compress
conn_params = {
'host': self.remote_host,
'port': self.port,
'username': self.username,
'cnopts': cnopts
}
if self.password and self.password.strip():
conn_params['password'] = self.password
if self.key_file:
conn_params['private_key'] = self.key_file
if self.private_key_pass:
conn_params['private_key_pass'] = self.private_key_pass
self.conn = pysftp.Connection(**conn_params)
return self.conn
示例2: write
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def write(self, dump_path, group_key=None, file_name=None):
import pysftp
if group_key is None:
group_key = []
filebase_path, file_name = self.create_filebase_name(group_key, file_name=file_name)
destination = (filebase_path + '/' + file_name)
self.logger.info('Start uploading to {}'.format(dump_path))
with pysftp.Connection(self.sftp_host, port=self.sftp_port,
username=self.sftp_user,
password=self.sftp_password) as sftp:
if not sftp.exists(filebase_path):
sftp.makedirs(filebase_path)
progress = SftpUploadProgress(self.logger)
sftp.put(dump_path, destination, callback=progress)
self.last_written_file = destination
self._update_metadata(dump_path, destination)
self.logger.info('Saved {}'.format(dump_path))
示例3: get_file_sftp
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def get_file_sftp(host, path_to_file, sftp_configuration):
"""
Copy an existing file from SFTP via sftp://*host*/*path_to_file* link to a home directory.
The function return the full path to the file that has been downloaded.
"""
# disable host key checking
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# construct SFTP object and get the file on a server
with pysftp.Connection(host, username = sftp_configuration["user"], password = sftp_configuration["psswd"],
cnopts = cnopts) as sftp:
sftp.get(path_to_file)
file_location = gc_write_dir + "/" + re.findall("[^/]*$", path_to_file)[0]
print("File " + path_to_file + " has got successfully.")
return file_location
示例4: _check_write_consistency
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def _check_write_consistency(self):
import pysftp
with pysftp.Connection(self.sftp_host, port=self.sftp_port,
username=self.sftp_user,
password=self.sftp_password) as sftp:
for file_info in self.get_metadata('files_written'):
try:
sftp_info = sftp.stat(file_info['filename'])
except IOError as e:
if e.errno == errno.ENOENT:
raise InconsistentWriteState(
'{} file is not present at destination'.format(file_info['filename']))
sftp_size = sftp_info.st_size
if sftp_size != file_info['size']:
raise InconsistentWriteState('Wrong size for file {}. Expected: {} - got {}'
.format(file_info['filename'], file_info['size'],
sftp_size))
self.logger.info('Consistency check passed')
示例5: sftpc
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def sftpc(self):
args_use = self.provider.args
if len(self.args):
args_use = self.args
kwargs_use = {}
kwargs_use['host'] = self.host
kwargs_use['port'] = int(self.port) if self.port else 22
for k, v in self.provider.kwargs.items():
kwargs_use[k] = v
for k, v in self.kwargs.items():
kwargs_use[k] = v
conn = pysftp.Connection(*args_use, **kwargs_use)
yield conn
conn.close()
示例6: _sftp_connect
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def _sftp_connect(self):
try:
self.conn.pwd
return True
except:
try:
import pysftp
except ImportError:
raise ModuleNotFoundError('Please install pysftp to use SFTP.')
self.conn = pysftp.Connection(self.host, username=self.username, password=self.password,
port=self.port, **self.pysftp_conn_kwargs)
示例7: connect
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def connect(self):
"""
Establishes the actual connection to the referred RSE.
:param: credentials needed to establish a connection with the stroage.
:raises RSEAccessDenied: if no connection could be established.
"""
try:
self.rse['credentials']['host'] = self.attributes['hostname']
self.__connection = pysftp.Connection(**self.rse['credentials'])
except Exception as e:
raise exception.RSEAccessDenied(e)
示例8: setupClass
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def setupClass(cls):
"""SFTP (RSE/PROTOCOLS): Creating necessary directories and files """
# Creating local files
cls.tmpdir = tempfile.mkdtemp()
cls.user = uuid()
with open("%s/data.raw" % cls.tmpdir, "wb") as out:
out.seek((1024 * 1024) - 1) # 1 MB
out.write('\0')
for fil in MgrTestCases.files_local:
os.symlink('%s/data.raw' % cls.tmpdir, '%s/%s' % (cls.tmpdir, fil))
# Load local credentials from file
with open('etc/rse-accounts.cfg') as fil:
data = json.load(fil)
credentials = data['LXPLUS']
lxplus = pysftp.Connection(**credentials)
with open('etc/rse_repository.json') as fil:
prefix = json.load(fil)['LXPLUS']['protocols']['supported']['sftp']['prefix']
lxplus.execute('mkdir %s' % prefix)
lxplus.execute('dd if=/dev/urandom of=%s/data.raw bs=1024 count=1024' % prefix)
cls.static_file = 'sftp://lxplus.cern.ch:22%sdata.raw' % prefix
protocol = mgr.create_protocol(mgr.get_rse_info('LXPLUS'), 'write')
for fil in MgrTestCases.files_remote:
tmp = protocol.parse_pfns(protocol.lfns2pfns({'name': fil, 'scope': 'user.%s' % cls.user}).values()[0]).values()[0]
for cmd in ['mkdir -p %s' % ''.join([tmp['prefix'], tmp['path']]), 'ln -s %sdata.raw %s' % (prefix, ''.join([tmp['prefix'], tmp['path'], tmp['name']]))]:
lxplus.execute(cmd)
lxplus.close()
示例9: tearDownClass
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def tearDownClass(cls):
"""SFTP (RSE/PROTOCOLS): Removing created directorie s and files """
# Load local creditentials from file
credentials = {}
with open('etc/rse-accounts.cfg') as fil:
data = json.load(fil)
credentials = data['LXPLUS']
lxplus = pysftp.Connection(**credentials)
with open('etc/rse_repository.json') as fil:
prefix = json.load(fil)['LXPLUS']['protocols']['supported']['sftp']['prefix']
lxplus.execute('rm -rf %s' % prefix)
lxplus.close()
shutil.rmtree(cls.tmpdir)
示例10: update_connection
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def update_connection(self, login, session=None):
connection = (session.query(Connection).
filter(Connection.conn_id == "sftp_default")
.first())
old_login = connection.login
connection.login = login
session.commit()
return old_login
示例11: test_get_conn
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def test_get_conn(self):
output = self.hook.get_conn()
self.assertEqual(type(output), pysftp.Connection)
示例12: test_no_host_key_check_default
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def test_no_host_key_check_default(self, get_connection):
connection = Connection(login='login', host='host')
get_connection.return_value = connection
hook = SFTPHook()
self.assertEqual(hook.no_host_key_check, False)
示例13: test_no_host_key_check_enabled
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def test_no_host_key_check_enabled(self, get_connection):
connection = Connection(
login='login', host='host',
extra='{"no_host_key_check": true}')
get_connection.return_value = connection
hook = SFTPHook()
self.assertEqual(hook.no_host_key_check, True)
示例14: test_no_host_key_check_disabled
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def test_no_host_key_check_disabled(self, get_connection):
connection = Connection(
login='login', host='host',
extra='{"no_host_key_check": false}')
get_connection.return_value = connection
hook = SFTPHook()
self.assertEqual(hook.no_host_key_check, False)
示例15: test_no_host_key_check_ignore
# 需要导入模块: import pysftp [as 别名]
# 或者: from pysftp import Connection [as 别名]
def test_no_host_key_check_ignore(self, get_connection):
connection = Connection(
login='login', host='host',
extra='{"ignore_hostkey_verification": true}')
get_connection.return_value = connection
hook = SFTPHook()
self.assertEqual(hook.no_host_key_check, True)