Monday, 19 January 2015

File operation in Python using FTP

In real world of internet we know the value of file transfer between places, whether it be within one room, building or city or even countries. When the time comes of using FTP in web programming Python Language seems to be one of the best solution. Here are three programs demonstrating 



i.  File upload to remote server.
ii  Retrieve directory listing from remote server to confirm file upload and
iii Read the uploaded file.

-------------------------------------------------------------------------------------------------------------------------
File upload to remote server.

# program  includes 2 library ftplib and sys, ftplib is for ftp related operation and sys is for command 
# related operation
import ftplib
import sys




# argv is the argument vector which is an array storing arguments passed by the user with the 
# program name. i.e. argv[0] is the program name, argv[1] is the argument (here the filename to be 
# uploaded

filename = sys.argv[1]


#ftplib.FTP connects to the remote server via ftp and returns connection object to connect.

connect = ftplib.FTP("192.168.0.109")

# connect object calls login function to ftp login to server using credential as we use during ftp login 
# from command line.

connect.login("oracle","oracle")
# sys library is used here to open the file to be uploaded, returning object to file.

file = open(filename,"rb")

#storbinary function of connect object actually fires an ftp command STOR filename which acutally 
#uploads the file to server ftp root directory.

connect.storbinary("STOR " + filename,file)
# connection terminated
connect.quit()



# local file closed.
file.close()

-------------------------------------------------------------------------------------------------------------------------

Retrieve directory listing from remote server to confirm file upload

#Purpose of ftplib, ftplib.FTP and connect.login is described above.

import ftplib
connect = ftplib.FTP("192.168.0.109")
connect.login("oracle","oracle")



# data variable initialized to an empty array.
data = []

# connect.dir function is called which returns the directory listing (same as when we type ls -l 
# command in linux or dir in windows) and appended to data array object.
connect.dir(data.append)
# data is traversed in a loop line by line and printed into console 
for line in data:
    print(line)


 Output will be something like this
ftp python







-------------------------------------------------------------------------------------------------------------------------
Read the uploaded file.

import ftplib
import sys

filename = sys.argv[1]
connect = ftplib.FTP("192.168.0.109")
connect.login("oracle","oracle")




# This line is actually retrieving content of the remote file line by line firing an ftp command RETR 
# filename

connect.retrlines("RETR " + filename)
 

connect.quit()

This is a console based programming, but actual objective is to show that this program can be implemented in web programming for file operation.

You can watch the video tutorial on youtube on this lesson : https://www.youtube.com/watch?v=GvPM0Uuqa98

No comments: