4

I am learning to retrieve files from an ftp server using ftplib from this link : https://docs.python.org/2/library/ftplib.html

When i run this code

from ftplib import FTP
ftp = FTP('ftp.debian.org')
ftp.login()

I get

TimeoutError: [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

From this answer https://stackoverflow.com/questions/4946960/when-using-ftplib-in-python#= i get to know that this is a server side issue which can be fixed by changing to ACTV mode.

So i changed my code to

from ftplib import FTP
ftp = FTP()
ftp.set_pasv(True)
ftp.connect('ftp.debian.org')
ftp.login()

Still same error. Can anyone tell me what other reasons could there be from my problem?

Edit - Using Python 3.6.1 on Thonny(IDE) in a 64 bit Win 10 environment

Community
  • 1
  • 1
  • try [this](http://stackoverflow.com/q/27953923/5513005) it's a different approach, might help you – Yash Karanke May 22 '17 at 06:47
  • Same code works in the online python compiler at https://www.tutorialspoint.com/execute_python_online.php So the issue must be with my setup. – Aseem September May 22 '17 at 06:59
  • @YashKaranke Thats not an issue with my code since the ftp here is part of the URL and not just the protocol. Full URL was ftp://ftp.debian.org so i have already removed the ftp:// part. – Aseem September May 22 '17 at 07:02
  • 1
    I changed my internet connection to my mobile hotspot and the code has no issues. Can you tell me if my router is blocking the requests or my ISP is? – Aseem September May 22 '17 at 14:04
  • @AseemSeptember : Did you figure out what's the issue? I am facing the same problem. – iamai Mar 11 '19 at 03:39

3 Answers3

0

Nothing wrong with this code. It works for me. Maybe the server was just very slow at the time you tried it. You can set a timeout in the connect:

ftp.connect('ftp.debian.org',timeout=seconds)
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • I have run this code on multiple servers for a number of times. I get the same error each time, thats why i think the issue is with my setup. I tied turning the firewall off but that doesnt help. – Aseem September May 22 '17 at 07:10
  • 1
    I used the timeout=120 but even then i receive the same error in just 15 seconds. – Aseem September May 22 '17 at 07:15
0

I had the same problem. As the ftplib description Passive mode is on by default. So set it to False. It works for me.

ftp.set_pasv(False)

Or active passive in server(it's Debian 11 for me) as define the min & max port:

pasv_min_port=10000
pasv_max_port=11000

Of course, you need add the ports in firewall:

ufw allow 10000:11000/tcp
Ryan Xu
  • 1
  • 4
0

Setting passive mode to False works for me. Thanks!!

ftp.set_pasv(False)
thethiny
  • 1,125
  • 1
  • 10
  • 26