python ftp_Python FTP

本文介绍了如何使用Python的ftplib模块进行FTP操作,包括连接FTP服务器、登录、获取欢迎信息、查看当前目录、列举目录、切换目录、发送命令、下载文件以及关闭连接。示例代码详细展示了每个操作的实现过程。
摘要由CSDN通过智能技术生成

python ftp

Today we will learn about python ftp operations. FTP is an acronym for File Transfer Protocol. Just like what it sounds, this protocol is used to transfer files across a network from source to destination machine.

今天,我们将学习python ftp操作。 FTP是文件传输协议的缩写。 就像听起来一样,此协议用于在网络上从源计算机到目标计算机传输文件。

In this post on python ftp module, we will see how to connect to a FTP server, upload & download files and much more. Let’s get started.

在python ftp模块的这篇文章中,我们将看到如何连接到FTP服务器,上传和下载文件等等。 让我们开始吧。

Python的FTP (Python ftp)

With Python, ftplib module provides all the functions which we might need to perform actions across the FTP protocol. Let’s start exploring this lesson with simple connection attempt.

使用Python, ftplib 模块提供了我们可能需要跨FTP协议执行操作的所有功能。 让我们通过简单的连接尝试开始本课。

使用login()函数连接到FTP服务器 (Connecting to an FTP server with login() function)

We will start by making a connection to an FTP server. We will use a server publicly available for usage, ftp.cse.buffalo.edu. Feel free to choose any server, just make sure you select the right server otherwise you will face connection errors.

我们将从建立与FTP服务器的连接开始。 我们将使用可供使用的ftp.cse.buffalo.edu服务器ftp.cse.buffalo.edu 。 随意选择任何服务器,只需确保选择正确的服务器,否则将面临连接错误。

We will write a simple script to connect to the said server:

我们将编写一个简单的脚本来连接到所述服务器:

from ftplib import FTP

# Host to connect to
host = 'ftp.cse.buffalo.edu'

# Make an Python FTP object and anonymously login
ftp = FTP(host)
print(ftp.login())

See how we can login without a username and a password? That is supported by few servers only. Let us see the output:

ftplib-connection

In this example, we started by importing a single class from complete module called FTP. We used its object to connect to a host and anonymously login to the server.
Of course, we didn’t pass a port to connect. This means just like any other request that this will arrive at the default port of the server. To change this, specify the port for the connection as well:

看看我们如何在没有用户名和密码的情况下登录? 仅少数服务器支持该功能。 让我们看一下输出:

在此示例中,我们从完整的模块FTP导入单个类开始。 我们使用其对象连接到主机并匿名登录到服务器。
当然,我们没有通过端口进行连接。 这意味着,就像其他任何请求一样,它将到达服务器的默认端口。 要更改此设置,请同时指定连接端口:

from ftplib import FTP

# Host to connect to
host = 'ftp.cse.buffalo.edu'
port = 8099

# Make an FTP object and anonymously login
ftp = FTP(host, port)
print(ftp.login())

Do take care that the port you’re connecting to actually supports an incoming FTP connection.

请注意,您要连接的端口实际上支持传入的FTP连接。

使用getwelcome()函数感到欢迎 (Feel welcomed with getwelcome() function)

A server can make you feel welcomed by returning a message. This message can be obtained using getwelcome() function. Easy to use, let’s put it in an example:

服务器可以通过返回一条消息使您感到满意。 可以使用getwelcome()函数获取此消息。 易于使用,我们举个例子:

from ftplib import FTP

# Host to connect to
host = 'ftp.cse.buffalo.edu'

# Make an FTP object and anonymously login
ftp = FTP(host)
print(ftp.login())
print(ftp.getwelcome())

Output for this script will just show a simple message which can be anything:

ftp-getwelcome

该脚本的输出将仅显示一条简单的消息,可以是任何内容:

使用pwd()函数显示目录 (Present Directory with pwd() function)

We can get the path of presently working directory of the server our connection is present at currently:

我们可以获得当前连接所在的服务器当前工作目录的路径:

from ftplib import FTP

host = 'ftp.cse.buffalo.edu'
ftp = FTP(host)
ftp.login()

ftp.cwd('mirror')
print(ftp.pwd())

Output for this script will just show present working directory:

ftplib-pwd

该脚本的输出将仅显示当前的工作目录:

使用retrlines()函数访问目录 (Accessing Directories with retrlines() function)

Now, just like an open SSH shell, we can use ftplib to access the directories on the server, navigate through them and change them based on needs.

现在,就像一个开放的SSH shell一样,我们可以使用ftplib访问服务器上的目录,进行浏览并根据需要进行更改。

from ftplib import FTP

# Host to connect to
host = 'ftp.cse.buffalo.edu'

# Make an FTP object and anonymously login
ftp = FTP(host)
print(ftp.login())

# List directories in current path
print(ftp.retrlines('LIST'))

Look at the last line, we used the retrlines(...) function to list the current directories. Output for this script will be:

ftplib-directories

看最后一行,我们使用了retrlines(...)函数列出了当前目录。 该脚本的输出为:

使用cwd()函数更改目录 (Changing Directories with cwd() function)

In above example, we listed the directories our script was currently accessing. To add, we can see current directory easily as:

在上面的示例中,我们列出了脚本当前正在访问的目录。 要添加,我们可以很容易地看到当前目录:

from ftplib import FTP

# Host to connect to
host = 'ftp.cse.buffalo.edu'

# Make an FTP object and anonymously login
ftp = FTP(host)
print(ftp.login())

# List directories in current path
print(ftp.retrlines('LIST'))

# Change into one of the sub-directories
ftp.cwd('mirror')
print(ftp.retrlines('LIST'))

Here, we’re switching to one of the sub-directories we noticed in an earlier run and we get the following results:

ftplib-cwd

The LIST attribute passed just pulls out the files and folders in current directory along with their information, which we then print.

在这里,我们将切换到先前运行中注意到的子目录之一,并得到以下结果:

传递的LIST属性只是将当前目录中的文件和文件夹连同其信息一起拉出,然后我们将其打印出来。

使用sendcmd()函数将命令发送到服务器 (Send command to server with sendcmd() function)

Using the sendcmd() function, we can send a simple String command to the server and obtain the String response. For an example, we will send a command STAT which can check the status fo a server:

使用sendcmd()函数,我们可以向服务器发送一个简单的String命令并获取String响应。 例如,我们将发送一条命令STAT,该命令可以检查服务器的状态:

from ftplib import FTP

host = 'ftp.cse.buffalo.edu'
ftp = FTP(host)
print(ftp.login())

# Check server status
print(ftp.sendcmd('STAT'))

When we run this script, we see the complete output on our own console:

ftplib-sendcmd

运行此脚本时,我们会在自己的控制台上看到完整的输出:

使用retrbinary()函数下载文件 (Downloading files with retrbinary() function)

Using ftpliob module, we can even download files locally. It is worth noticing that to do this, you must have proper access to a server and directories and file names and exact paths should be known.

使用ftpliob模块,我们甚至可以在本地下载文件。 值得注意的是,您必须对服务器具有正确的访问权限,并且目录和文件名以及确切的路径应该是已知的。

We will simply access a file on server and download it locally:

我们将只访问服务器上的文件并在本地下载:

from ftplib import FTP

host = 'ftp.cse.buffalo.edu'
ftp = FTP(host)
ftp.login()

ftp.cwd('CSE421')
print(ftp.retrlines('LIST'))

out = '/Users/shubham/README.txt'
with open(out, 'wb') as f:
    ftp.retrbinary('RETR ' + 'README.txt', f.write)

I add some print statements in between to make the output a bit more clear. Let’s run this program now:

python ftp download file

And here is the file which was downloaded:

Please note that you will need to modify the location of file download based on your local machine path before running this code.

我在两者之间添加了一些打印语句,以使输出更加清晰。 让我们现在运行该程序:

python ftp下载文件

这是下载的文件:

请注意,在运行此代码之前,您将需要根据本地计算机路径修改文件下载的位置。

使用close()函数关闭连接 (Close connection with close() function)

We should close the ftp connection once we’re done with any tasks needed to do be done:

完成所有需要完成的任务后,我们应该关闭ftp连接:

from ftplib import FTP

host = 'ftp.cse.buffalo.edu'
ftp = FTP(host)
ftp.login()

ftp.cwd('mirror')
print(ftp.pwd())

ftp.close()
print('Connection closed.')

Output for this script will just show present working directory:

ftplib-close

Remember, we cannot open a closed python ftp connection again.

该脚本的输出将仅显示当前的工作目录:

请记住,我们无法再次打开已关闭的python ftp连接。

In this lesson, we learned about various ways through which we can access an FTP server and play with directories and manage it completely.

在本课程中,我们学习了各种方法,通过这些方法可以访问FTP服务器并播放目录并对其进行完全管理。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/17533/python-ftp

python ftp

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值