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'
Related
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 am posting the question and the answer, I found, as well, incase it would help someone. The following were my minimum requirements:
1. Client machine is Windows 10 and remote server is Linux
2. Connect to remote server via SSH through HTTP Proxy
3. HTTP Proxy uses Basic Authentication
4. Run commands on remote server and display output
The purpose of the script was to login to the remote server, run a bash script (check.sh) present on the server and display the result. The Bash script simply runs a list of commands displaying the overall health of the server.
There have been numerous discussions, here, on how to implement HTTP Proxy or running remote commands using Paramiko. However, I could not find the combination of both.
from urllib.parse import urlparse
from http.client import HTTPConnection
import paramiko
from base64 import b64encode
# host details
host = "remote-server-IP"
port = 22
# proxy connection & socket definition
proxy_url = "http://uname001:passw0rd123#HTTP-proxy-server-IP:proxy-port"
url = urlparse(proxy_url)
http_con = HTTPConnection(url.hostname, url.port)
auth = b64encode(bytes(url.username + ':' + url.password,"utf-8")).decode("ascii")
headers = { 'Proxy-Authorization' : 'Basic %s' % auth }
http_con.set_tunnel(host, port, headers)
http_con.connect()
sock = http_con.sock
# ssh connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname=host, port=port, username='remote-server-uname', password='remote-server-pwd', sock=sock)
except paramiko.SSHException:
print("Connection Failed")
quit()
stdin,stdout,stderr = ssh.exec_command("./check")
for line in stdout.readlines():
print(line.strip())
ssh.close()
I would welcome any suggestions to the code as I am a network analyst and not a coder but keen to learn and improve.
I do not think that your proxy code is correct.
For a working proxy code, see How to ssh over http proxy in Python?, particularly the answer by #tintin.
As it seems that you need to authenticate to the proxy, after the CONNECT command, add a Proxy-Authorization header like:
Proxy-Authorization: Basic <credentials>
where the <credentials> is base-64 encoded string username:password.
cmd_connect = "CONNECT {}:{} HTTP/1.1\r\nProxy-Authorization: Basic <credentials>\r\n\r\n".format(*target)
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')
I need to login ftps server in windows,am attaching the code which i tried to make a connection with ftps.
from ftplib import FTP_TLS
ftps = FTP_TLS('my host addres',990)
ftps.login('username','password')
ftps.prot_p()
ftps.retrlines('LIST')
when i execute this code am getting a socket error no 10060.i knew my ftp connection is implicit.I am very new to python.so please anyone help me out to solve this issue.
here is the answer for my question.in python there is one module called chilkat with that i can able to login into my implicit ftps server.
import sys
import chilkat
ftp = chilkat.CkFtp2()
# Any string unlocks the component for the 1st 30-days.
success = ftp.UnlockComponent("Anything for 30-day trial")
if (success != True):
print(ftp.lastErrorText())
sys.exit()
# If this example does not work, try using passive mode
# by setting this to True.
ftp.put_Passive(False)
ftp.put_Hostname("ur host ip")
ftp.put_Username("usename")
ftp.put_Password("passowrd")
ftp.put_Port(990)
# We don't want AUTH SSL:
ftp.put_AuthTls(False)
# We want Implicit SSL:
ftp.put_Ssl(True)
# Connect and login to the FTP server.
success = ftp.Connect()
if (success != True):
print(ftp.lastErrorText())
sys.exit()
else:
# LastErrorText contains information even when
# successful. This allows you to visually verify
# that the secure connection actually occurred.
print(ftp.lastErrorText())
print("FTPS Channel Established!")
# Do whatever you're doing to do ...
# upload files, download files, etc...
localFilename = "c:/temp/hamlet.xml"
remoteFilename = "hamlet.xml"#the file name which u download from the ftps
# Download a file.
success = ftp.GetFile(remoteFilename,localFilename)
if (success != True):
print(ftp.lastErrorText())
ftp.Disconnect()
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.