Maintain server-initiated FTP connection between client requests?

51 views
Skip to first unread message

Jordon Wing

unread,
Dec 28, 2011, 8:51:54 PM12/28/11
to django...@googlegroups.com
Hey,
I'm going to use ftplib to open up an FTP connection to an FTP server provided by the user.  The client will be sending FTP commands to my django server via Ajax, which will then be forwarded to the FTP server the user provided. However, I'd like to not have to create a new FTP server connection every time the client sends an FTP command. In other words, I want to keep the FTP connection alive between requests by the client.

How would I do this? Would some sort of comet implementation be best? I was initially planning to use WebSockets until I discovered my host won't allow it. =\

francescortiz

unread,
Dec 29, 2011, 11:36:51 AM12/29/11
to Django users
You will have to create a connection pool as a resident thread.
Briefly, a connection pool will take care of:
- Create a new connection if there is no connection available.
- Reuse a connection if possible.
- Remove connections that are no longer being used or that commanded
to close.

Then, your code will have have to communicate with the connection
pool. Example code:

# The connection_pool is smart enough to reuse a connection if it is
in the pool
ftp_connection = connection_pool.get_connection(server, user, pass)

current_dir = ftp_connection.getcwd()
dir_contents = ftp_connection.ls()
ftp_connection.rm(file)


You have a similar implementation here:
http://forum.codecall.net/python-tutorials/41844-object-pooling-python.html

Jordon Wing

unread,
Dec 29, 2011, 7:00:34 PM12/29/11
to django...@googlegroups.com
Thanks! That looks to be exactly what I needed. I'll try implementing that and post on here if I have any further questions but it looks rather straightforward.
Reply all
Reply to author
Forward
0 new messages