Using threading to keep FTP control port alive

2019-04-30 01:49发布

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!

1条回答
Deceive 欺骗
2楼-- · 2019-04-30 02:23

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?

查看更多
登录 后发表回答