python3+ftplib实现ftp客户端
一、程序说明
1.1 程序实现关键点
python实现ftp客户端,主要会遇到以下四个问题:
第一个问题是使用什么包实现----我们这里是使用标准库中的ftplib
第二个问题是怎么连接登录ftp服务器----如果是ssh那么直接使用connect函数就直接完成连接和登录两项工作,而ftp要先用connect连接然后再用login登录
第三个问题是怎么实现ftp操作----怎么实现ftp操作这个问题有点麻烦,我们平时使用ftp命令登录后就可以输入下图中的命令进行ftp操作,对于其中少数一些命令ftplib确实支持通过sendcmd方法将命令传过去然后服务端就执行然后返回执行结果,但对于大多数命令ftplib都是通过一个方法去实现的(比如cd命令对应cwd
()方法,mkd
ir命令对应mkd()方法等,参见1.2
)
第四个问题是怎么实现文件的上传下载----文件上传下载其实也是ftp操作的一部份,如上面所说ftplib基本是每个命令都使用一个方法去实现,而对于上传的put命令对应的就是storbinary方法,对于下载的get命令对应的就是retrbinary方法。具体参数和用法可参看下边程序源代码
1.2 常用ftp命令与函数对应关系
全部方法及方法参数解析可参看官方文档
ftp命令 | 对应的FTP对象方法 | 备注 |
ls | nlst([pathname]) | Return a list of file names as returned by the NLST command. |
dir | dir([pathname]) | Produce a directory listing as returned by the LIST command, printing it to standard output. |
rename | rename(fromname, toname) | Rename file fromname on the server to toname. |
delete | delete(filename) | Remove the file named filename from the server. |
cd | cwd(pathname) | Set the current directory on the server. |
mkdir | mkd(pathname) | Create a new directory on the server. |
pwd | pwd() | Return the pathname of the current directory on the server. |
rmdir | rmd(dirname) | Remove the directory named dirname on the server. |
size(filename) | Request the size of the file named filename on the server. | |
quit | quit() | Send a QUIT command to the server and close the connection. |
close | close() | Close the connection unilaterally. |
put | storbinary(cmd, fp, blocksize=8192, callback=None, rest=None) | Store a file in binary transfer mode. |
get | retrbinary(cmd, callback, blocksize=8192, rest=None) | Retrieve a file in binary transfer mode. |
1.3 程序截图
程序执行结果截图:
二、程序源代码
import logging
from ftplib import FTP class MyFtp():
def __init__(self):
self.ftp_client = FTP() # 些函数实现ftp登录
def ftp_login(self,host_ip,username,password):
try:
self.ftp_client.connect(host_ip,port=21,timeout=10)
except :
logging.warning('network connect time out')
return 1001
try:
self.ftp_client.login(user=username, passwd=password)
except:
logging.warning('username or password error')
return 1002
return 1000 # 此函数执行ftp命令,并打印命令执行结果
def execute_some_command(self):
# 通运sendcmd方法形式执行pwd命令,为使用形式统一起见不推荐使用此种形式,而且其实大多数命令都是支持这种形式的
command_result = self.ftp_client.sendcmd('pwd')
logging.warning('command_result:%s'% command_result)
# 通过直接使用pwd方法执行pwd命令,推荐统一使用此种形式
command_result = self.ftp_client.pwd()
logging.warning('command_result:%s' % command_result)
# 上传文件;'stor ftp_client.py'告诉服务端将上传的文件保存为ftp_client.py,open()是以二进制读方式打开本地要上传的文件
command_result = self.ftp_client.storbinary('stor ftp_client.py',open("ftp_client.py",'rb'))
logging.warning('command_result:%s' % command_result)
# 下载文件;'retr .bash_profile'告诉服务端要下载服务端当前目录下的.bash_profile文件,open()是以二进制写方式打开本地要存成的文件
command_result = self.ftp_client.retrbinary('retr .bash_profile', open('local_bash_profile', 'wb').write)
logging.warning('command_result:%s' % command_result) # 此函数实现退出ftp会话
def ftp_logout(self):
logging.warning('now will disconnect with server')
self.ftp_client.close() if __name__ == '__main__':
# 要连接的主机ip
host_ip = '192.68.220.128'
# 用户名
username = 'ls'
# 密码
password = 'abcd1234'
# 实例化
my_ftp = MyFtp()
# 如果登录成功则执行命令,然后退出
if my_ftp.ftp_login(host_ip,username,password) == 1000:
logging.warning('login success , now will execute some command')
my_ftp.execute_some_command()
my_ftp.ftp_logout()
参考:
https://docs.python.org/3/library/ftplib.html
pwd
()
python3+ftplib实现ftp客户端的更多相关文章
- Python的网络编程[1] -> FTP 协议[2] -> 使用 ftplib 建立 FTP 客户端
使用 ftplib 建立 FTP 客户端 用于建立FTP Client,与 pyftplib 建立的 Server 进行通信. 快速导航 1. 模块信息 2. 建立 FTP 客户端 1. 模块信息 1 ...
- CentOS7 安装Python3,开发SocketIO 客户端
CentOS7安装Python3,开发SocketIO 客户端 参考:https://blog.csdn.net/lovefengruoqing/article/details/79284573 步骤 ...
- 【转载】HTTP/FTP客户端开发库:libwww、libcurl、libfetch
网页抓取和ftp访问是目前很常见的一个应用需要,无论是搜索引擎的爬虫,分析程序,资源获取程序,WebService等等都是需 要的,自己开发抓取库当然是最好了,不过开发需要时间和周期,使用现有的Ope ...
- 【python】FTP客户端
Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,函数列举如下 ftp登陆连接 from ftplib import FTP #加 ...
- 【HTTP/FTP客户端库】
[HTTP/FTP客户端库]资料来源:http://curl.haxx.se/libcurl/competitors.html Free Software and Open Source projec ...
- 使用 Socket 通信实现 FTP 客户端程序(来自IBM)
FTP 客户端如 FlashFXP,File Zilla 被广泛应用,原理上都是用底层的 Socket 来实现.FTP 客户端与服务器端进行数据交换必须建立两个套接字,一个作为命令通道,一个作为数据通 ...
- Socket网络编程--FTP客户端
Socket网络编程--FTP客户端(1)(Windows) 已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解F ...
- ftp客户端命令使用简记
OS:windows8.1评估版 程序和功能 tftp客户端勾选上 Win+R:运行,键入cmd,键入ftp -help 如下图: 使用ftp客户端可以做的事:将文件传送到运行FTP服务器服务(经常称 ...
- Socket网络编程--FTP客户端(1)(Windows)
已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解FTP作用 就是一个提供一个文件的共享协议. 1.了解FTP协议 ...
随机推荐
- Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- JaveWeb 公司项目(6)----- 通过ToolTip给控件添加动态注释
现在公司的项目进展到了视屏这一块,关于海康网页端的构建我会另外写一篇博客来详细讲解,这一篇的博文主要讲的是我刚刚遇到的一个小问题 连接上了视屏之后,将控制按钮换成图标,方位按钮比较好理解,调焦调距的按 ...
- P1262 间谍网络
传送门 思路: ①在 Tarjan 的基础上加一个 belong 记录每个点属于哪个强连通分量. ②存图完成后,暴力地遍历全图,查找是否要间谍不愿受贿. inline void dfs(int u) ...
- Centos7:查看某个端口被哪个进程占用
查看端口被哪个进程使用的命令 netstat -lnp | grep 参考: https://blog.csdn.net/u010886217/article/details/83626236 htt ...
- 《剑指offer》第五十七题(为s的连续正数序列)
// 面试题57(二):为s的连续正数序列 // 题目:输入一个正数s,打印出所有和为s的连续正数序列(至少含有两个数). // 例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以结 ...
- Java中的包扫描(工具)
在现在好多应用场景中,我们需要得到某个包名下面所有的类, 包括我们自己在src里写的java类和一些第三方提供的jar包里的类,那么怎么来实现呢? 今天带大家来完成这件事. 先分享代码: 1.这个类是 ...
- KMP算法(next数组方法)
KMP算法之前需要说一点串的问题: 串: 字符串:ASCII码为基本数据形成的一堆线性结构. 串是一个线性结构:它的存储形式: typedef struct STRING { CHARACTER *h ...
- Lua --- 输入一个数字,输出阶乘
function fact(n) == n then else ) end end print("Enter a number : ") a = io.read("*nu ...
- Appium解决搜索框问题
appium解决搜索框: 1. 点击搜索,手工测试会弹出键盘,需要点击键盘上的搜索按钮. 2.但自动化的时候,键盘不能弹出.所以我们可以用回车等keycode代替搜索按钮. Press Keycode ...
- Word Ladder(双向BFS)
2018-10-02 23:46:38 问题描述: 问题求解: 显然是个解空间遍历问题,每次修改其中一位,由于步长是1,所以可以使用BFS进行解空间的遍历.