python code to connect to sftp server

2024/7/7 6:11:37

I found this code to connect to remote sftp server with the help of username ,password and host but i also need to include the port number, can any one let em know how to include the port number in this code and also for this piece of code 'parmiko.util.log_to_file(log_filename)' what should i hard code for log_filename ??Iam runnign this code in unix environment.

import os
import paramiko
server, username, password = ('host', 'username', 'password')   
ssh = paramiko.SSHClient()  
parmiko.util.log_to_file(log_filename)    
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())' #In case the server's key is unknown,'
#we will be adding it automatically to the list of known hosts 
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))   #Loads the user's local known host file  
ssh.connect(server, username=username, password=password) 
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp') print "output", ssh_stdout.read() #Reading output of the executed co'mmand 
error = ssh_stderr.read()  #Reading the error stream of the executed command
print "err", error, len(error) #Transfering files to and from the remote machine' 
sftp = ssh.open_sftp()   
'sftp.get(remote_path, local_path)'
sftp.put(local_path, remote_path) 
sftp.close()
ssh.close()
Answer

There is a port= named argument to the ssh.connect() method. See the manual

Example:

ssh.connect(server, port=portnumber, username=username, password=password)
https://en.xdnf.cn/q/120029.html

Related Q&A

Python: get the max value with the location above and below than the max

If I have a dataframe like this, index User Value location1 1 1.0 4.5 2 1 1.5 5.23 1 3.0 7.04 1 2.5 7.55 2 1.0 11.56 2 1.…

Retrieve smart cards PAN with Python and pyscard

Im trying to retrieve the PAN of a smart card using pyscard in Python. What I have done so far is to connect to the reader and to retrieve various information about the reader and the card... but I can…

How to stop a specific key from working in Python

My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didnt work, I tried programming a code that can stop it but i couldnt because i am a beginner…

How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

trying to write a Python function: def compare_lengths(x, y, z) which takes as arguments three arrays and checks their lengths and returns them as a triple in order of length. For example, if the funct…

How to split a string in Python by 2 or 3, etc [duplicate]

This question already has answers here:Split string every nth character(21 answers)How to iterate over a list in chunks(40 answers)Closed 10 years ago.Does anyone know if its possible in python to spli…

.LAS into a .CSV file using python

How to change a .las file into a .csv file? Have been trying myself but no luck no far. I am just looking for something decently short that will save some time when I have to convert big .olas files i…

using pandas read_excel to read from stdin

Note: I have solve this problem as per below:I can use to_csv to write to stdout in python / pandas. Something like this works fine:final_df.to_csv(sys.stdout, index=False)I would like to read in an a…

How to print a string x times based on user input [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Understanding for loops in Python(4 answers)Closed 1 year ago.I am trying to make a simple application that will pri…

How does .join work in Python?

I want to display each row of a SQL query result on a webpage. I found some code, but I dont understand what this line does.u"<br>".join([u"{0}".format(row.combination) for r…

Login, Navigate and Retrieve data behind a proxy with Python

I want, with a python script, to be able to login a website and retrieve some data. This behind my companys proxy.I know that this question seems a duplicate of others that you can find searching, but …