Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using threading to keep FTP control port alive

My FTP download script (based on ftplib) is hanging on large downloads. The remote server I'm working with, while highly configurable (proftpd) is unable to get passed the firewall limitations imposed by the remote network.

I am restricted to using passive mode at my company, i.e. IT will not open a port for me to allow FTP active mode.

So, I'm stuck with passive mode.

The remote server gets disconnected 10 to 15 minutes in to a large download.

I NEED TO keep the control connection alive during a large download. Should I use threading, sending a 'NOOP' command every ten seconds? If so, would you kind souls send me some example code that would do the trick? I need to pass multiple values to my FTP download function. The keepalive function would not need any parameters passed to it.

Or, is there a way to control the specific socket that I'm using on port 21 (control connection) to keep it alive while the large download takes place on port nnnnnn?

Thanks much!

like image 209
EmBee Avatar asked Mar 11 '11 04:03

EmBee


1 Answers

You might also try to use the TCP KeepAlive feature by adding this code:

import socket
ftp.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

The support of the TCP KeepAlive is highly platform and firewall dependent - thus no guarantee, but it is designed for this purpose and it would be a lot simpler than adding an additional thread. On which platforms are you working?

like image 131
weismat Avatar answered Sep 23 '22 16:09

weismat