Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'pysftp' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
def sftp_mock():
return MagicMock(autospec=pysftp.Connection)
def test_mkdir(self, sftp_conn, sftp_client, tmpdir):
"""Tests the `mkdir` method with mode parameter."""
dir_path = posixpath.join(str(tmpdir), "subdir")
assert not sftp_client.exists(dir_path)
with SftpHook("sftp_default") as hook:
hook.mkdir(dir_path, mode=0o750)
assert sftp_client.exists(dir_path)
assert pysftp.st_mode_to_int(sftp_client.stat(dir_path).st_mode) == 750
def test_makedirs(self, sftp_conn, sftp_client, tmpdir):
"""Tests the `mkdir` method with mode parameter."""
dir_path = posixpath.join(str(tmpdir), "some", "nested", "dir")
with SftpHook("sftp_default") as hook:
hook.makedirs(dir_path, mode=0o750)
assert sftp_client.exists(dir_path)
assert pysftp.st_mode_to_int(sftp_client.stat(dir_path).st_mode) == 750
bucket_create(auth, project.id, destination['storage']['bucket'])
# put the file
file_out = destination['storage']['bucket'] + ':' + destination['storage']['path'] + variant
if project.verbose: print('SAVING', file_out)
object_put(auth, file_out, rows_to_csv(rows))
if 'sftp' in destination:
try:
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
path_out, file_out = destination['sftp']['file'].rsplit('.', 1)
file_out = path_out + variant + file_out
sftp = pysftp.Connection(host=destination['sftp']['host'], username=destination['sftp']['username'], password=destination['sftp']['password'], port=destination['sftp']['port'], cnopts=cnopts)
if '/' in file_out:
dir_out, file_out = file_out.rsplit('/', 1)
sftp.cwd(dir_out)
sftp.putfo(rows_to_csv(rows), file_out)
except e:
print(str(e))
traceback.print_exc()
with open(ignfile, 'r', encoding='UTF-8') as fr:
lines = fr.read().split('\n')
while '' in lines:
lines.remove('')
except FileNotFoundError:
lines = []
# Establish SFTP connection
try:
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
print("Connecting to %s:%s... " % (host, port), end='')
if key != '':
sftp = pysftp.Connection(host, username=user, port=int(port), private_key=key, cnopts=cnopts)
else:
sftp = pysftp.Connection(host, username=user, port=int(port), password=password, cnopts=cnopts)
print("Connected.")
except pysftp.exceptions.ConnectionException:
print("\n\nAn error occurred when establishing connection.\nCheck for Internet connection.")
exit(8)
except paramiko.ssh_exception.AuthenticationException:
print("\n\nAuthentication failed.")
exit(7)
# Check for uploading directory
print("Checking for %s... " % path, end='')
if sftp.exists(path):
print("Exist.")
print("Change directory to %s... Done." % path)
sftp.cd(path)
else:
# Remote directory do not exist
}
if rsa_key_path:
params["private_key"] = self.rsa_key_path
if rsa_key_passphrase:
params["private_key_pass"] = rsa_key_passphrase
else:
params["password"] = password
cnopts = pysftp.CnOpts()
if sftp_public_key:
key = paramiko.RSAKey(data=base64.b64decode(sftp_public_key))
cnopts.hostkeys.add(server, 'ssh-rsa', key)
else:
cnopts.hostkeys = None
with pysftp.Connection(**params, cnopts=cnopts) as sftp:
# set keepalive to prevent socket closed / connection dropped error
sftp._transport.set_keepalive(30)
try:
sftp.chdir(path)
except IOError:
# Create directory and subdirs if they do not exist.
currentDir = ''
for dirElement in path.split('/'):
currentDir += dirElement + '/'
try:
sftp.chdir(currentDir)
except Exception as e:
print(('(Part of the) path doesn\'t exist. Creating it now at ' + currentDir))
# Make directory and then navigate into it
sftp.mkdir(currentDir, mode=777)
rsa_key_path = ICPSudo.get_param('saas_server.rsa_key_path', None)
rsa_key_passphrase=ICPSudo.get_param('saas_server.rsa_key_passphrase')
sftp_public_key=ICPSudo.get_param('saas_server.sftp_public_key')
params = {
"host": server,
"username": username,
}
if rsa_key_path:
params["private_key"] = self.rsa_key_path
if rsa_key_passphrase:
params["private_key_pass"] = rsa_key_passphrase
else:
params["password"] = password
cnopts = pysftp.CnOpts()
if sftp_public_key:
key = paramiko.RSAKey(data=base64.b64decode(sftp_public_key))
cnopts.hostkeys.add(server, 'ssh-rsa', key)
else:
cnopts.hostkeys = None
with pysftp.Connection(**params, cnopts=cnopts) as sftp:
# set keepalive to prevent socket closed / connection dropped error
sftp._transport.set_keepalive(30)
try:
sftp.chdir(path)
except IOError:
# Create directory and subdirs if they do not exist.
currentDir = ''
for dirElement in path.split('/'):
makedirs_safe(parse_path(file_out))
with open(file_out, 'w') as save_file:
save_file.write(rows_to_csv(rows).read())
if 'storage' in destination and destination['storage'].get('bucket') and destination['storage'].get('path'):
# create the bucket
bucket_create(auth, project.id, destination['storage']['bucket'])
# put the file
file_out = destination['storage']['bucket'] + ':' + destination['storage']['path'] + variant
if project.verbose: print('SAVING', file_out)
object_put(auth, file_out, rows_to_csv(rows))
if 'sftp' in destination:
try:
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
path_out, file_out = destination['sftp']['file'].rsplit('.', 1)
file_out = path_out + variant + file_out
sftp = pysftp.Connection(host=destination['sftp']['host'], username=destination['sftp']['username'], password=destination['sftp']['password'], port=destination['sftp']['port'], cnopts=cnopts)
if '/' in file_out:
dir_out, file_out = file_out.rsplit('/', 1)
sftp.cwd(dir_out)
sftp.putfo(rows_to_csv(rows), file_out)
except e:
print(str(e))
traceback.print_exc()
site_name = input("Enter the site name to publish ")
username = input("Enter tableau user name ")
password = getpass.getpass("Please enter your password ")
data_file_name = str(data_file).rsplit('.tde', 1)[0]
subprocess.run(["tabcmd", "--accepteula"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = subprocess.run(["tabcmd", "login", "-s",
"{}".format(site_name), "-u", "{}".format(username),
"-p", "{}".format(password)], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if result.returncode != 0:
raise ConnectionException("Unable to connect to tableau server")
result = subprocess.run(["tabcmd", "publish",
"{}".format(data_file),
"-r", "{}".format(project_name)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
if ("A data source named '{}' already exists in project".format(
data_file_name) in str(result.stderr)):
result = subprocess.run(
["tabcmd", "publish", "{}".format(data_file),
"-r", "{}".format(project_name), "--overwrite"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
overwrite = True
if "Unexpected response from the server:" not in str(result.stderr):
print(result.stderr)
raise SSHException("Unable to get response from tableau server")
while '' in lines:
lines.remove('')
except FileNotFoundError:
lines = []
# Establish SFTP connection
try:
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
print("Connecting to %s:%s... " % (host, port), end='')
if key != '':
sftp = pysftp.Connection(host, username=user, port=int(port), private_key=key, cnopts=cnopts)
else:
sftp = pysftp.Connection(host, username=user, port=int(port), password=password, cnopts=cnopts)
print("Connected.")
except pysftp.exceptions.ConnectionException:
print("\n\nAn error occurred when establishing connection.\nCheck for Internet connection.")
exit(8)
except paramiko.ssh_exception.AuthenticationException:
print("\n\nAuthentication failed.")
exit(7)
# Check for uploading directory
print("Checking for %s... " % path, end='')
if sftp.exists(path):
print("Exist.")
print("Change directory to %s... Done." % path)
sftp.cd(path)
else:
# Remote directory do not exist
# Print every file not marked as 'ignored'
total_size = 0