Python - ftplib.FTP_TLS Port - python

Can someone help me confirm the default port when using ftplib.FTP_TLS? We have opened port 990 and 21 but my script fails to connect.
import ftplib
session = ftplib.FTP_TLS('xxx.ftp.com','user','password')
file = open('Bkup.tar.gz','rb')
session.storbinary('STOR Bkup.tar.gz', file)
file.close()
session.quit()
Thank You!

You could try:
ftplib.FTP_TLS.port = 21

According to it's own documentation, as well as the spec, FTPS (or FTP over TLS) connects to port 21.
You appear to be missing a login clause to authenticate the session.
Try calling session.login() followed by session.prot_p() before attempting to store the binary.
This documentation can be found by using the help function or in the online documentation here.
I hope that helps.

Please don't modify the port directly. If you get a closer look at FTP_TLS super __init__ you can clearly see that if it's called with a host param connect gets automatically called with just the host as param so the port defaults to FTP_PORT which is 21.
A better approach would be to call FTP_TLS without any params and then manually call the connect method where you can specify host as well as port.
from ftplib import FTP_TLS
client = FTP_TLS()
client.connect(host="ftp.example.com", port=12121)

Oddly, the FTP_TLS object has no direct port parameter on the constructor like the FTP object does.
you can set the port prior to calling the constructor and that works.
ftplib.FTP_TLS.port=1234
ftplib.FTP_TLS( 'ftphost.domain.tld', 'user', 'password' )

Related

FTP connection working on FileZilla and command line but not python ftplib

I have an ftp server deployed on aws. I can connect to it from FileZilla as well as from the command line. However, the python script I have gives me:
OSError: [Errno 101] Network is unreachable
I know that my script works since I can connect to other servers.
def test_connection():
server = ftplib.FTP()
server.connect('xx.xxx.xxx.xx')
server.login('xxxx', 'xxx')
print(server.dir())
server.quit()
if __name__ == "__main__":
test_connection()
Any clues what this could be ?
Many thanks :)
You may want to restructure your code so that credentials are initialized in the FTP constructor call.
import ftplib
server= ftplib.FTP('server.address.com','USERNAME','PASSWORD')
print(server.dir())
session.quit()
Use ftplib.FTP_TLS instead if you FTP host requires TLS.
What worked in the end:
I changed the vsftpd config:
listen=YES
listen_ipv6=NO
Not exactly sure though what the problem is

With pysftp, how can I specify a timeout value for the connection?

With pysftp, I see how to set a timeout for any commands once you're already connected, but I don't see how to set a timeout for the connection itself. I feel like I'm missing something somewhere. Just to try it, I tried adding timeout=3 to the Connection method and got an error, and tried using cnopts.timeout=3 and that did nothing at all. For the record, I'm using Python 3 on Windows, if that affects anything.
Here's some simple test code you can experiment with, if it helps. (As is, the connection times out after about 30 seconds.)
import pysftp
print("\n"*25)
cnopts=pysftp.CnOpts()
# - I know this next line is insecure, it's just for this test program.
cnopts.hostkeys = None
print('Connecting...')
# - 8.8.8.8 is Google's public DNS server. It doesn't respond to sftp requests at all,
# - so it's a good test case for how long a connection timeout takes.
with pysftp.Connection('8.8.8.8', username='anonymous', password='password',
cnopts=cnopts) as SFTP:
print("Wait, how did you get this far?")
print("Done.")
It does not look like that pysftp allows setting a connection timeout.
You can use Paramiko directly instead (pysftp is just a wrapper around Paramiko).
Paramiko SSHClient.connect method has timeout parameter.
ssh = paramiko.SSHClient()
ssh.connect(host, username=username, password=password, timeout=timeout)
sftp = ssh.open_sftp()
you can do it with:
Connection.timeout (35000)

How to configure pyftpdlib to create an 'active' only FTP server?

I need to create a test case for a FTP client that involves connecting to a server that only accepts 'active' FTP connections. For other cases I am using pyftpdlib, and it works like charm, but I can't see an easy way to configure it to behave just in FTP active mode, and not passive.
Thanks.
If by what you wrote in the title you mean "literaly disable PASV (passive)" mode you can just tell pyftpdlib to not interpret that command. Not tested:
from pyftpdlib.ftpserver import FTPHandler
handler = FTPHandler
del handler.proto_cmds['PASV']
del handler.proto_cmds['EPSV']
...
This way pyftpdlib will reject any PASV/EPSV request with "550 Command PASV not understood.".

Python FTP to iPad

I'm on Windows 7.
I cannot connect to my iPad with a simple Python script:
HOST = '192.168.1.122'
try:
f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror), e:
MessageBox.Show('ERROR: cannot reach "%s"' % HOST)
return
try:
f.connect(HOST,2121)
f.login()
except ftplib.error_perm:
MessageBox.Show('ERROR: cannot login anonymously')
f.quit()
return
The errors I have is "getaddrinfo returns an empty list" and the "cannot reach..." message... Cannot solve it...
I tried to FTP with several programs on the iPad without success. If I FTP via DOS box or using a FTP software it works. I tried as well another FTP server on my PC and it works.
I am forced to use port 2121, so can't change it.
Any clue or experience?
You should read docs before anything:
class ftplib.FTP([host[, user[,
passwd[, acct[, timeout]]]]]) Return a
new instance of the FTP class. When
host is given, the method call
connect(host) is made. When user is
given, additionally the method call
login(user, passwd, acct) is made
(where passwd and acct default to the
empty string when not given). The
optional timeout parameter specifies a
timeout in seconds for blocking
operations like the connection attempt
(if is not specified, the global
default timeout setting will be used).
So, if you do f = ftplib.FTP(HOST) it fails because it will try to connect to standard port (21) and not 2121.
You should get an instance of ftplib and later use f.connect(HOST, 2121).
http://docs.python.org/library/ftplib.html

SFTP using ftplib

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.

Categories