Skip to content

Commit 8da22e8

Browse files
committed
added downloading & uploading files in ftp server tutorial
1 parent 4f2c207 commit 8da22e8

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
8787
- [How to Use Threads to Speed Up your IO Tasks in Python](https://www.thepythoncode.com/article/using-threads-in-python). ([code](python-standard-library/using-threads))
8888
- [How to List all Files and Directories in FTP Server using Python](https://www.thepythoncode.com/article/list-files-and-directories-in-ftp-server-in-python). ([code](python-standard-library/listing-files-in-ftp-server))
8989
- [How to Read Emails in Python](https://www.thepythoncode.com/article/reading-emails-in-python). ([code](python-standard-library/reading-email-messages))
90+
- [How to Download and Upload Files in FTP Server using Python](https://www.thepythoncode.com/article/download-and-upload-files-in-ftp-server-using-python). ([code](python-standard-library/download-and-upload-files-in-ftp))
9091

9192
- ### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
9293
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [How to Download and Upload Files in FTP Server using Python](https://www.thepythoncode.com/article/download-and-upload-files-in-ftp-server-using-python)
2+
- To upload a file to a FTP server, consider editing `ftp_file_uploader.py` on your needs and run it.
3+
- To download a file, same in `ftp_file_downloader.py`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import ftplib
2+
3+
FTP_HOST = "ftp.dlptest.com"
4+
FTP_USER = "dlpuser@dlptest.com"
5+
FTP_PASS = "SzMf7rTE4pCrf9dV286GuNe4N"
6+
7+
# connect to the FTP server
8+
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
9+
# force UTF-8 encoding
10+
ftp.encoding = "utf-8"
11+
# the name of file you want to download from the FTP server
12+
filename = "some_file.txt"
13+
with open(filename, "wb") as file:
14+
# use FTP's RETR command to download the file
15+
ftp.retrbinary(f"RETR {filename}", file.write)
16+
17+
# quit and close the connection
18+
ftp.quit()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import ftplib
2+
3+
# FTP server credentials
4+
FTP_HOST = "ftp.dlptest.com"
5+
FTP_USER = "dlpuser@dlptest.com"
6+
FTP_PASS = "SzMf7rTE4pCrf9dV286GuNe4N"
7+
8+
# connect to the FTP server
9+
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
10+
# force UTF-8 encoding
11+
ftp.encoding = "utf-8"
12+
# local file name you want to upload
13+
filename = "some_file.txt"
14+
with open(filename, "rb") as file:
15+
# use FTP's STOR command to upload the file
16+
ftp.storbinary(f"STOR {filename}", file)
17+
# list current files & directories
18+
ftp.dir()
19+
# quit and close the connection
20+
ftp.quit()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a text file!

Diff for: python-standard-library/listing-files-in-ftp-server/list_files.py

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
FTP_HOST = "ftp.ed.ac.uk"
66
FTP_USER = "anonymous"
77
FTP_PASS = ""
8-
# FTP_HOST = "192.168.1.1"
9-
# FTP_USER = "admin"
10-
# FTP_PASS = "586290929699"
118

129
# some utility functions that we gonna need
1310
def get_size_format(n, suffix="B"):

0 commit comments

Comments
 (0)