Giao thức truyền tệp [FTP] là một giao thức mạng tiêu chuẩn để chuyển các tệp đến và từ máy chủ. Python có một mô -đun gọi là ftplib
cho phép chuyển các tệp thông qua giao thức FTP. is a standard network protocol for the transfer of files to and from the server. Python has a module called ftplib
that allows the transfer of files through the FTP protocol.
Mô -đun ftplib
cho phép chúng tôi triển khai phía máy khách của giao thức FTP và nó cho phép người dùng kết nối với máy chủ để gửi và nhận tệp.
Kết nối với máy chủ FTP
Mã sau đây trình bày cách kết nối với máy chủ FTP và tạo một thể hiện của lớp.
Chúng tôi sẽ kết nối với một máy chủ FTP có sẵn công khai - hàm
0 hiển thị thông báo ban đầu được gửi bởi máy chủ trên kết nối.import ftplib
ftp = ftplib.FTP["ftp.nluug.nl"]
ftp.login["anonymous", "ftplib-example-1"]
# The .dir[] function gets the directories on the server
files = ftp.dir[]
print[files]
# The .cwd[] function displays the current working directory
# we are in.
print["Files in the /pub/ directory"]
ftp.cwd['/pub/'] #change directory to /pub
files = ftp.dir[]
print[files]
# The .pwd[] function gets the directory that we are in
current_directory = ftp.pwd[]
print["Current directory:", current_directory]
import ftplib
ftp = ftplib.FTP["ftp.nluug.nl"]
ftp.login["anonymous", "ftplib-example-1"]
print[ftp.getwelcome[]]
Liệt kê các tập tin và thư mục
Chúng ta có thể thấy các tập tin và thư mục có sẵn trên máy chủ. Mô -đun cũng cung cấp cho chúng tôi các lệnh để liệt kê thư mục hiện tại của chúng tôi và thay đổi thư mục hiện tại mà chúng tôi đang tham gia.
import ftplib
ftp = ftplib.FTP["ftp.nluug.nl"]
ftp.login["anonymous", "ftplib-example-1"]
# The .dir[] function gets the directories on the server
files = ftp.dir[]
print[files]
# The .cwd[] function displays the current working directory
# we are in.
print["Files in the /pub/ directory"]
ftp.cwd['/pub/'] #change directory to /pub
files = ftp.dir[]
print[files]
# The .pwd[] function gets the directory that we are in
current_directory = ftp.pwd[]
print["Current directory:", current_directory]
Tải xuống tệp văn bản thông qua FTP
Chúng ta có thể sử dụng mô -đun FTP để tải xuống các tệp. Thư mục
1 hiện tại mà chúng tôi đang ở cóimport ftplib
ftp = ftplib.FTP["ftp.nluug.nl"]
ftp.login["anonymous", "ftplib-example-1"]
# The .dir[] function gets the directories on the server
files = ftp.dir[]
print[files]
# The .cwd[] function displays the current working directory
# we are in.
print["Files in the /pub/ directory"]
ftp.cwd['/pub/'] #change directory to /pub
files = ftp.dir[]
print[files]
# The .pwd[] function gets the directory that we are in
current_directory = ftp.pwd[]
print["Current directory:", current_directory]
2 mà chúng tôi sẽ truy xuất mã của mình bằng hàmimport ftplib
ftp = ftplib.FTP["ftp.nluug.nl"]
ftp.login["anonymous", "ftplib-example-1"]
# The .dir[] function gets the directories on the server
files = ftp.dir[]
print[files]
# The .cwd[] function displays the current working directory
# we are in.
print["Files in the /pub/ directory"]
ftp.cwd['/pub/'] #change directory to /pub
files = ftp.dir[]
print[files]
# The .pwd[] function gets the directory that we are in
current_directory = ftp.pwd[]
print["Current directory:", current_directory]
3. Mã lưu tệp trên máy tính cục bộ của chúng tôi và sau đó đóng kết nối bằng hàmimport ftplib
ftp = ftplib.FTP["ftp.nluug.nl"]
ftp.login["anonymous", "ftplib-example-1"]
# The .dir[] function gets the directories on the server
files = ftp.dir[]
print[files]
# The .cwd[] function displays the current working directory
# we are in.
print["Files in the /pub/ directory"]
ftp.cwd['/pub/'] #change directory to /pub
files = ftp.dir[]
print[files]
# The .pwd[] function gets the directory that we are in
current_directory = ftp.pwd[]
print["Current directory:", current_directory]
4.import ftplib
ftp = ftplib.FTP["ftp.nluug.nl"]
ftp.login["anonymous", "ftplib-example-1"]
# The .dir[] function gets the directories on the server
files = ftp.dir[]
print[files]
# The .cwd[] function displays the current working directory
# we are in.
print["Files in the /pub/ directory"]
ftp.cwd['/pub/'] #change directory to /pub
files = ftp.dir[]
print[files]
# The .pwd[] function gets the directory that we are in
current_directory = ftp.pwd[]
print["Current directory:", current_directory]
import ftplib
ftp = ftplib.FTP["ftp.nluug.nl"]
ftp.login["anonymous", "ftplib-example-1"]
ftp.cwd['/pub/'] #change directory to /pub/
files = ftp.dir[]
print[files]
# Downloading the robots.txt file
filename='robots.txt'
ftp.retrbinary["RETR " + filename ,open[filename, 'wb'].write]
ftp.quit[]
Người đóng góp
Mohammad Razi ul Haq
Cổng nào là dịch vụ FTP hoạt động? or File Transfer Protocol is a well-known network protocol used to transfer files between computers in a network. It is created on client server architecture and can be used along with user authentication. It can also be used without authentication but that will be less secure. FTP connection which maintains a current working directory and other flags, and each transfer requires a secondary connection through which the data is transferred. Most common web browsers can retrieve files hosted on FTP servers.
Giao thức FTP thường sử dụng cổng 21 làm phương tiện giao tiếp chính của nó. Một máy chủ FTP sẽ lắng nghe các kết nối máy khách trên cổng 21.
FTP hoặc giao thức truyền tệp là một giao thức mạng nổi tiếng được sử dụng để chuyển các tệp giữa các máy tính trong một mạng. Nó được tạo trên kiến trúc máy chủ máy khách và có thể được sử dụng cùng với xác thực người dùng. Nó cũng có thể được sử dụng mà không cần xác thực nhưng điều đó sẽ kém an toàn. Kết nối FTP duy trì một thư mục làm việc hiện tại và các cờ khác và mỗi lần chuyển yêu cầu kết nối thứ cấp thông qua đó dữ liệu được truyền. Hầu hết các trình duyệt web phổ biến có thể truy xuất các tệp được lưu trữ trên các máy chủ FTP.ftplib which has the below required methods to list the files as we will transfer the files.
pwd [] | Thư mục làm việc hiện tại. |
cwd [] | Thay đổi thư mục làm việc hiện tại thành đường dẫn. |
Dir [[Path [, ... [, CB]]] | Hiển thị danh sách thư mục của đường dẫn. CB gọi lại tùy chọn được chuyển đến Retrines []. |
Storlines [CMD, F] | Tải lên tệp văn bản bằng FTP CMD đã cho - ví dụ: lưu trữ tên tệp. |
Storbinary [CMD, F [, BS = 8192]]] | Tương tự như Storlines [] nhưng được sử dụng cho các tệp nhị phân. |
Xóa [đường dẫn] | Xóa tệp từ xa nằm ở đường dẫn. |
MKD [Thư mục] | Tạo thư mục từ xa. |
Ngoại lệ ftplib.error_temp | Ngoại lệ được nêu ra khi mã lỗi biểu thị lỗi tạm thời [mã phản hồi trong phạm vi 400 Lỗi499] được nhận .. |
Ngoại lệ ftplib.error_perm | Ngoại lệ được nêu ra khi mã lỗi biểu thị lỗi vĩnh viễn [mã phản hồi trong phạm vi 500 Lỗi599] được nhận .. |
Kết nối [máy chủ [, cổng [, thời gian chờ]]]] | Kết nối với máy chủ và cổng đã cho. Số cổng mặc định là 21, như được chỉ định bởi giao thức FTP .. |
từ bỏ[] | Đóng kết nối và thoát. |
Dưới đây là các ví dụ của một số phương pháp trên.
Liệt kê các tập tin
Ví dụ dưới đây sử dụng đăng nhập ẩn danh vào máy chủ FTP và liệt kê nội dung của thư mục hiện tại. Nó xử lý thông qua tên của các tập tin và thư mục và lưu trữ chúng như một danh sách. Sau đó in chúng ra.
import ftplib ftp = ftplib.FTP["ftp.nluug.nl"] ftp.login["anonymous", "ftplib-example-1"] data = [] ftp.dir[data.append] ftp.quit[] for line in data: print "-", line
Khi chúng tôi chạy chương trình trên, chúng tôi nhận được đầu ra sau -
- lrwxrwxrwx 1 0 0 1 Nov 13 2012 ftp -> . - lrwxrwxrwx 1 0 0 3 Nov 13 2012 mirror -> pub - drwxr-xr-x 23 0 0 4096 Nov 27 2017 pub - drwxr-sr-x 88 0 450 4096 May 04 19:30 site - drwxr-xr-x 9 0 0 4096 Jan 23 2014 vol
Thay đổi thư mục
Chương trình dưới đây sử dụng phương thức CWD có sẵn trong mô -đun FTPLIB để thay đổi thư mục và sau đó lấy nội dung cần thiết.
import ftplib ftp = ftplib.FTP["ftp.nluug.nl"] ftp.login["anonymous", "ftplib-example-1"] data = [] ftp.cwd['/pub/'] change directory to /pub/ ftp.dir[data.append] ftp.quit[] for line in data: print "-", line
Khi chúng tôi chạy chương trình trên, chúng tôi nhận được đầu ra sau -
- lrwxrwxrwx 1 504 450 14 Nov 02 2007 FreeBSD -> os/BSD/FreeBSD - lrwxrwxrwx 1 504 450 20 Nov 02 2007 ImageMagick -> graphics/ImageMagick - lrwxrwxrwx 1 504 450 13 Nov 02 2007 NetBSD -> os/BSD/NetBSD - lrwxrwxrwx 1 504 450 14 Nov 02 2007 OpenBSD -> os/BSD/OpenBSD - -rw-rw-r-- 1 504 450 932 Jan 04 2015 README.nluug - -rw-r--r-- 1 504 450 2023 May 03 2005 WhereToFindWhat.txt - drwxr-sr-x 2 0 450 4096 Jan 26 2008 av - drwxrwsr-x 2 0 450 4096 Aug 12 2004 comp
Thay đổi thư mục
Chương trình dưới đây sử dụng phương thức CWD có sẵn trong mô -đun FTPLIB để thay đổi thư mục và sau đó lấy nội dung cần thiết.getfile method. This method moves a copy of the file from the remote system to the local system from where the ftp connection was initiated.
import ftplib import sys def getFile[ftp, filename]: try: ftp.retrbinary["RETR " + filename ,open[filename, 'wb'].write] except: print "Error" ftp = ftplib.FTP["ftp.nluug.nl"] ftp.login["anonymous", "ftplib-example-1"] ftp.cwd['/pub/'] change directory to /pub/ getFile[ftp,'README.nluug'] ftp.quit[]
Tìm nạp các tập tin