Skip to content

Commit b4ddd09

Browse files
Update P76_PythonFTP.py
1 parent 62b5b7d commit b4ddd09

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

Programs/P76_PythonFTP.py

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
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
1+
# Author: OMKAR PATHAK (Annotated Version)
252

263
import ftplib
4+
import os
275

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()
6+
7+
def ftp_upload(ftp, local_path, remote_path):
8+
# ❌ VIOLATION: Original used storlines for binary file
9+
# ftpObj.storlines()
10+
11+
# ✅ FIX
12+
with open(local_path, 'rb') as f:
13+
ftp.storbinary(f'STOR {remote_path}', f)
14+
15+
16+
def main():
17+
# ❌ VIOLATION: Hardcoded credentials (SECURITY RISK)
18+
# ftp.login('omkarpathak', '8149omkar')
19+
20+
# ✅ FIX: Use environment variables
21+
host = os.getenv("FTP_HOST", "127.0.0.1")
22+
user = os.getenv("FTP_USER")
23+
password = os.getenv("FTP_PASS")
24+
25+
if not user or not password:
26+
print("Set FTP_USER and FTP_PASS")
27+
return
28+
29+
try:
30+
ftp = ftplib.FTP(host)
31+
ftp.login(user, password)
32+
33+
ftp_upload(ftp, "output.txt", "output.txt")
34+
35+
except Exception as e:
36+
# ❌ VIOLATION: No error handling in original
37+
print("Error:", e)
38+
39+
finally:
40+
ftp.quit()
41+
42+
43+
if __name__ == "__main__":
44+
main()

0 commit comments

Comments
 (0)