-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathP76_PythonFTP.py
More file actions
44 lines (30 loc) · 982 Bytes
/
P76_PythonFTP.py
File metadata and controls
44 lines (30 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Author: OMKAR PATHAK (Annotated Version)
import ftplib
import os
def ftp_upload(ftp, local_path, remote_path):
# ❌ VIOLATION: Original used storlines for binary file
# ftpObj.storlines()
# ✅ FIX
with open(local_path, 'rb') as f:
ftp.storbinary(f'STOR {remote_path}', f)
def main():
# ❌ VIOLATION: Hardcoded credentials (SECURITY RISK)
# ftp.login('omkarpathak', '8149omkar')
# ✅ FIX: Use environment variables
host = os.getenv("FTP_HOST", "127.0.0.1")
user = os.getenv("FTP_USER")
password = os.getenv("FTP_PASS")
if not user or not password:
print("Set FTP_USER and FTP_PASS")
return
try:
ftp = ftplib.FTP(host)
ftp.login(user, password)
ftp_upload(ftp, "output.txt", "output.txt")
except Exception as e:
# ❌ VIOLATION: No error handling in original
print("Error:", e)
finally:
ftp.quit()
if __name__ == "__main__":
main()