I'm writing a script that will connect to a remote FTP server and login using my username and password and then upload a file from my HDD to the server.
I feel like there can be alternatives to hardcoding my sensitive information (password and username) in the code. Any suggestions? What's the best thing that can be done?
import ftplib
ftp = ftplib.FTP('ftp.mydomain.com')
ftp.login('username', 'password')
Related
i am getting really confused on how to connect to an ftp server while using a proxy in python. Some previous examples have confused me a lot as i am new to Python. Below are the variables i believe that will be needed in the code sample (the proxy is only a example for security reasons as is the username and password).
can someone please show me how to connect to the ftp server and download a file
the end goal is to use python to download files from this ftp server
import ftplib
proxy = {'http': "auth-proxy.xxxx.com:232", 'https': "auth-proxy.xxx.com:232"}
ftp_site = 'https://data.ftse.com'
username = 'xxxxxx'
password = 'yyyyyy'
#file path where the file is on the ftp server is below
filepath = 'https://data.ftse.com/filedownloadservlet?filename=eerf1808.csv&phyName=nXVi8I4q5Boy8gBpdpDg3jNBqfAZy0vfydRBHC3zs3qt6YadMA6xa9LM3QaIgx-g'
I have a python app that runs on an SSH connection through MobaXTerm. Many people use the app, the current version has each one log in with a username & password. I am trying to skip the login process all together and just have a list of authorized users' windows login usernames. The only ways I know how to retrieve the username will return the username of the current SSH connection which is the same for everyone. Is there a way to get the Windows username during an SSH connection?
os.getlogin()
os.path.expanduser('~')
os.environ.get('USERNAME')
getpass.getuser()
pwd.getpwuid(os.getuid())[0]
These all return the SSH username. Any help would be much appreciated.
I am trying to upload a file to an ftp server on my same wifi network to get a picture on to a digital picture frame. I succeeded in uploading through file explorer, but when uploading using a python script I get a 530 response.
Here is the code so far
import ftplib
ftp = ftplib.FTP()
ftp.connect("111.111.1.11", 1111) #dummy host and port
file = open('C:/path/to/file/test1.png','rb')
ftp.storbinary('test.png', file)
file.close()
ftp.quit()
The server does not requre me to log in with a username and password on file explorer, is there some sort of default I need?
530 error code means Authentication failed error so you are missing the authentication piece. Maybe you can do something like this:
ftp = FTP(source_address=("111.111.1.11", 1111))
ftp.login(user, password)
Note that if you don't provide a user and password it will login with:
user anonymous
password anonymous
as described here
Also I would recommend you reading about S-FTP (Secure FTP) because in FTP the credentials are passed in clear text in the login request.
S-FTP is a communication protocol similar to FTP but built on top of ssh.
Hope this helped you !
I use Paramiko to put a file to an SFTP server:
import paramiko
transport = paramiko.Transport((host, port))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(local_path, remote_path)
Now, I would like to check if it worked. The idea is that I compare the checksum of the local file and the remote one (that is located on the SFTP server).
Does Paramiko functionality allows to do that? If it is the case, how exactly it works?
With the SFTP, running over an encrypted SSH session, there's no chance the file contents could get corrupted while transferring. So unless it gets corrupted when reading the local file or writing the remote file, you can be pretty sure that the file was uploaded correctly, if the .put does not throw any error.
try:
sftp.put(local_path, remote_path)
except:
# Something went wrong
If you want to test explicitly anyway:
While there's the check-file extension to the SFTP protocol to calculate a remote file checksum, it's not widely supported. Particularly it's not supported by the most widespread SFTP server implementation, the OpenSSH. See What SFTP server implementations support check-file extension.
If you are lucky to connect to another SFTP server that supports the extension, you can use the Paramiko's SFTPFile.check method.
If not, your only option is to download the file back and compare locally.
If you have a shell access to the server, you can of course try to run some shell checksum command (sha256sum) over a separate shell/SSH connection (or channel) and parse the results. But that's not an SFTP solution anymore. See Comparing MD5 of downloaded files against files on an SFTP server in Python.
if the file doesn't upload then the method will throw an error, so u can check for error
import paramiko
transport = paramiko.Transport((host, port))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
try:
sftp.put(local_path, remote_path)
except IOError:
#'Failure'
except OSError:
#'Failure'
I need to download a file from a host using SFTP.
Do you know if is it possible to do that using Python ftplib?
I saw an example here, but when I try to connect I receive EOFError.
I tried this code:
import ftplib
ftp = ftplib.FTP()
ftp.connect( "1.2.3.4", "22" )
This method returns with an error after long time so I cannot perform a call to login.
I cannot try the constructor FTP([host[, user[, passwd[, acct[, timeout]]]]]) because
my port is 22 but ftplib default is 21.
If I follow the example
ftp = ftplib.FTP("1.2.3.4")
ftp = ftplib.FTP("1.2.3.4","22")
I receive a connection refused so I cannot enter any username password. Can you help me? Thank you very much
As the question you linked to states, ftplib doesn't support SFTP (which is a transfer protocol over SSH and has nothing to do with FTPS, FTP over SSL). Use the recommended Paramiko instead.