Skip to content

Commit 6ec2fe6

Browse files
committed
Python FTP file transfer example
1 parent 64ac202 commit 6ec2fe6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Programs/P76_PythonFTP.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Author: OMKAR PATHAK
2+
3+
# For transfering files to your another/local computer, you will have to install a FTP
4+
# Daemon. Execute following for doing the same:
5+
# 1. sudo apt-get install vsftpd
6+
# 2. service vsftpd start
7+
# 3. sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.orig
8+
# 4. sudo nano /etc/vsftpd.conf
9+
10+
# Now change the following settings in that file:
11+
#
12+
# anonymous_enable=NO # disable anonymous login
13+
# local_enable=YES # permit local logins
14+
# write_enable=YES # enable FTP commands which change the filesystem
15+
# local_umask=022 # value of umask for file creation for local users
16+
# dirmessage_enable=YES # enable showing of messages when users first enter a new directory
17+
# xferlog_enable=YES # a log file will be maintained detailing uploads and downloads
18+
# connect_from_port_20=YES # use port 20 (ftp-data) on the server machine for PORT style connections
19+
# xferlog_std_format=YES # keep standard log file format
20+
# listen=NO # prevent vsftpd from running in standalone mode
21+
# listen_ipv6=YES # vsftpd will listen on an IPv6 socket instead of an IPv4 one
22+
# pam_service_name=vsftpd # name of the PAM service vsftpd will use
23+
# userlist_enable=YES # enable vsftpd to load a list of usernames
24+
# tcp_wrappers=YES # turn on tcp wrappers
25+
26+
import ftplib
27+
28+
def ftp_upload(ftpObj, pathToSend, pathToRecv, fileType='TXT'):
29+
"""
30+
A function for uploading files to an FTP server
31+
@param ftpObj: The file transfer protocol object
32+
@param path: The path to the file to upload
33+
"""
34+
with open(pathToSend, 'rb') as fobj:
35+
ftpObj.storlines('STOR ' + pathToRecv, fobj)
36+
37+
if __name__ == '__main__':
38+
ftp = ftplib.FTP('127.0.0.1')
39+
ftp.login('omkarpathak', '8149omkar')
40+
print('Logged in..')
41+
42+
pathToSend = '/home/omkarpathak/Desktop/output.txt'
43+
pathToRecv = '/home/omkarpathak/Documents/output.txt'
44+
ftp_upload(ftp, pathToSend, pathToRecv)
45+
46+
ftp.quit()

0 commit comments

Comments
 (0)