Receiving files from an FTP Server

I’ve been asked for input on a method to receive XML files from an FTP server.
My hope is that this is something that has been included in a module of some sort but I don’t really see a good answer her or when I ask Mr Google. In the same request it was noted that the files could be placed in a shared folder and then that folder monitored for new files to retrieve, though I don’t think this is the preferred method. Server Message Block (SMB) and Microsoft Message Que (MSMQ) were also mentioned.

Any input is appreciated,
Mike

The python standard FTPlib is included with our bundled Jython:
https://docs.python.org/2/library/ftplib.html

1 Like

Here is a quick example, using an FTP test site. I prefer to extend from Java classes, as I have had better luck with that, but this works for my test.

from ftplib import FTP #Import the ftp class
import StringIO  #This gives us a string that looks like a file
s1 = StringIO.StringIO() #See above
s1.write("This is a test file\n")  #Write our test string to our "file"
s1.seek(0) #Set the buffer index back to 0.  If we don't, we can not read it
s2 = StringIO.StringIO()  #Create our output string "file"
pftp = FTP("ftp.dlptest.com") #Connect to our ftp site
pftp.login("dlpuser@dlptest.com","hZ3Xr8alJPl8TtE") #Login
pftp.storlines('STOR Kyle-Test2.txt',s1) #Create or overwrite the location on the server.  Write the context of s1 to that location
pftp.retrlines('LIST') #List the files on the server
pftp.retrlines('RETR Kyle-Test2.txt',s2.write) #Read the file from the server, writing the contents to our s2 string "file"
pftp.quit() #Close our ftp connection
print s2.getvalue() #Get the value of our string
3 Likes

Thanks for the suggestions.
Mike