Cannot establish SSL connection with FTP using certfile (python) - python

I'm trying to establish a connection with a FTP server using ftplib.
I received two files, a certificate with .crt extension and a certificate in PEM format (RSA KEY + Certificate).
I'm using the certificate in PEM format to create a context using the following code:
import ssl
from ftplib import FTP_TLS
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.load_cert_chain('./certificate3')
ftps = FTP_TLS(context = ssl_context)
ftps.connect(host, porta)
ftps.login(user = user, passwd = pwd)
ftps.prot_p()
ftps.nlst()
If I remove the last command (nlst) I receive the response: '230 Logged on'.
Therefore when I try to extract list of documents inside the FTP with nlst() command I receive an error message:
TimeoutError: [WinError 10060] A connection attempt failed because the connected component did not respond
correctly after a period of time or the established connection has failed
because the connected host did not respond
Does anybody know what is wrong and how to fix the code in order to establish the connection?

I could establish connection to FTP server with WinSCP.
Through WinSCP I can run an script to synchronize the server with my local directory. Below you can find the command:
option batch abort
option confirm off
open ftp://user:password#hostserver:port -hostkey="SHA256 KEY"
synchronize local local_path remote_path
exit
This command must be in .txt file, and through a .bat file this command is executed.
Hope I can help other with the same issue.

Related

FTP Connection has Limited Functions, Can't upload or list directory [duplicate]

I'm trying to connect to an FTP but I am unable to run any commands.
ftp_server = ip
ftp_username = username
ftp_password = password
ftp = ftplib.FTP(ftp_server)
ftp.login(ftp_username, ftp_password)
'230 Logged on'
ftp.nlst()
The ftp.nlst throws this error:
Error:
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I've tested the connection using FileZilla (running on the same machine) and it works fine.
This is FileZilla log:
Status: Connection established, waiting for welcome message...
Status: Insecure server, it does not support FTP over TLS.
Status: Logged in Status: Retrieving directory listing...
Status: Server sent passive reply with unroutable address. Using server address instead.
Status: Directory listing of "/" successful
Status: Server sent passive reply with unroutable address
The above means that the FTP server is misconfigured. It sends its internal network IP to outside network (to the client – FileZilla or Python ftplib), where it is invalid. FileZilla can detect that and automatically fall back to the original IP address of the server.
Python ftplib does not do this kind of detection.
You need to fix your FTP server to return the correct IP address.
If it is not feasible to fix the server (it's not yours and the admin is not cooperative), you can make ftplib ignore the returned (invalid) IP address and use the original address instead by overriding FTP.makepasv:
class SmartFTP(FTP):
def makepasv(self):
invalidhost, port = super(SmartFTP, self).makepasv()
return self.host, port
ftp = SmartFTP(ftp_server)
# the rest of the code is the same
In recent versions of Python (3.6 and newer), ftplib doesn't consider the IP address in PASV response on its own.
Another solution may be to use IPv6. See Python 3.8.5 FTPS connection.
For a different problem with similar consequences, see vsftpd returns 0,0,0,0 in response to PASV.

Cannot list FTP directory using ftplib – but FTP client works

I'm trying to connect to an FTP but I am unable to run any commands.
ftp_server = ip
ftp_username = username
ftp_password = password
ftp = ftplib.FTP(ftp_server)
ftp.login(ftp_username, ftp_password)
'230 Logged on'
ftp.nlst()
The ftp.nlst throws this error:
Error:
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I've tested the connection using FileZilla (running on the same machine) and it works fine.
This is FileZilla log:
Status: Connection established, waiting for welcome message...
Status: Insecure server, it does not support FTP over TLS.
Status: Logged in Status: Retrieving directory listing...
Status: Server sent passive reply with unroutable address. Using server address instead.
Status: Directory listing of "/" successful
Status: Server sent passive reply with unroutable address
The above means that the FTP server is misconfigured. It sends its internal network IP to outside network (to the client – FileZilla or Python ftplib), where it is invalid. FileZilla can detect that and automatically fall back to the original IP address of the server.
Python ftplib does not do this kind of detection.
You need to fix your FTP server to return the correct IP address.
If it is not feasible to fix the server (it's not yours and the admin is not cooperative), you can make ftplib ignore the returned (invalid) IP address and use the original address instead by overriding FTP.makepasv:
class SmartFTP(FTP):
def makepasv(self):
invalidhost, port = super(SmartFTP, self).makepasv()
return self.host, port
ftp = SmartFTP(ftp_server)
# the rest of the code is the same
In recent versions of Python (3.6 and newer), ftplib doesn't consider the IP address in PASV response on its own.
Another solution may be to use IPv6. See Python 3.8.5 FTPS connection.
For a different problem with similar consequences, see vsftpd returns 0,0,0,0 in response to PASV.

How do i retrieve files from ftp over HTTPs with python3

I am trying to retrieve files over and https connection using python and i cant seem to find any answers that work with my case
Ive tried using ftplib and FTP_TLS but im getting errors saying that my connection is refused. I log in using my provided credentials and am able to navigate to the actual folder.
>>> ftp = FTP_TLS('HOSTNAME')
>>> ftp.login('USERNAME', 'PASSWORD')
'230 User logged in, proceed.'
>>> ftp.prot_p()
'200 Command PROT okay.'
>>> ftp.cwd('outbox')
'250 Directory changed to /outbox'
>>> ftp.nlst()
Traceback ():
output
ConnectionRefusedError: [Errno 61] Connection refused
Unsure why im getting this when everything else has been successful but i am unable to access the files
I think your FTP server is configured to return an IP address that is not reachable by the Python script. Once a server reply comes to your Python script, the Python library tries to send data to a host that it doesn't have a route to (or maybe it doesn't even exist).
Some GUI programs try to mitigate that by ignoring the server's IP replies and using the hostname that the user entered during login, but libraries don't do that.
Check this: https://stumbles.id.au/python-ftps-and-mis-configured-servers.html

Getting "socket.error: [Errno 61] Connection refused" python paramiko

Getting error connection refused error when trying to connect to the host to copy a local file to the host server. Don't have any issue connecting to the server remotely though.
host = "9.29.22.222"
username = "XXX"
password = "XXX"
local_path = "/Users/samuelhii/Desktop/file.txt"
remote_path = "C:\Program Files (x86)\file.txt"
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(host,22,username,password)
sftp = s.open_sftp()
sftp.put(local_path,remote_path)
The connection was refused by the server. This can be caused by several reasons not related to Python programming:
a firewall
the SSH service is configure not to take requests from your IP
bad host ip
… (many more)
Check if you can use the normal SSH client to connect with this host/user/password combination.

login into local FTP server with ip

I am trying to log into a FTP server that I am only running locally in my network. To do this I have to use my ip address to use as the server address (see code below). However each time I get a gaierror: [Errno 11004] getaddrinfo failed error.
Can anyone look at my code and see if I am making any error that may be the cause of this address issue? Also I can log into the ftp server fine from my browser so I know the server is up and running correctly, also anonymous login to the server is allowed .
#import the ftp lib.
from ftplib import FTP
#enter the address of the ftp server to use, use ip address since server is ran locally
ftp = FTP('ftp://192.168.1.130')
#logs into the ftp server
ftp.login()
You're including the protocol with the hostname which is incorrect. What's happening is the library is trying to resolve "ftp://192.168.1.130" (instead of "192.168.1.130"), which isn't a valid address.
#Wrong
ftp = FTP('ftp://192.168.1.130')
#Right
ftp = FTP('192.168.1.130')
http://docs.python.org/library/ftplib.html
According to ftplib documentation, you shoud just give him the host address/IP, and not a string representing an URL. So here, you should simply do:
ftp = FTP('192.168.1.130')

Categories

Resources