码迷,mamicode.com
首页 > 编程语言 > 详细

Python的网络编程[1] -> FTP -> 使用 ftplib 建立 FTP 客户端

时间:2017-12-26 21:55:51      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:lib   python   pcl   ide   write   基于   目录   from   实例   

使用 ftplib 建立 FTP 客户端


用于建立FTP Client,与 pyftplib 建立的 Server 进行通信。

快速导航

1. 模块信息

2. 建立 FTP 客户端

 

1. 模块信息

1.1 常量 / Constants

       FTP_PORT = 21 标准的FTP协议服务端端口

1.2 / Class

1.2.1 FTP()

ftp = FTP()

无需参数传入直接生成FTP的客户端实例

1.2.1.1 connect()方法

函数调用: ftp.connect(address)

函数功能:用于客户端连接FTP服务器

传入参数: address

address: tuple类型,包含server IP和端口,address = (ip, port)

返回参数:

1.2.1.2 set_debuglevel()方法

函数调用: ftp.set_debuglevel(level)

函数功能:用于设置调试信息输出等级,分别有0,1,2级,0级不输出调试信息

传入参数: level

level: int类型,等级数字可以为0,1,2

返回参数:

1.2.1.3 login()方法

函数调用: ftp.login(user,  password)

函数功能:用于用户登录

传入参数: user, password

user: str类型,用户名

password: str类型,密码

返回参数:

1.2.1.4 getwelcome()方法

函数调用: ftp.getwelcome()

函数功能: 用于获取服务器的欢迎信息

传入参数:

返回参数: info

info: str类型,服务端banner设置的欢迎信息

1.2.1.5 retrbinary()方法

函数调用: ftp. retrbinary (cmd, file_handler, bufsize)

函数功能:用于从服务器上下载文件

传入参数: cmd, file_handler, bufsize

cmd: str类型,’RETR ’ + file_name, file_name为要下载的目标文件名

file_handler: type类型,常为open()函数以‘wb’方式打开的一个文件后的write方法,传入后会在内部进行调用,将数据写入

bufsize: int类型,常设置1024

返回参数:

1.2.1.6 storbinary()方法

函数调用: ftp. storbinary (cmd, file_handler, bufsize)

函数功能:用于向服务器上传文件

传入参数: cmd, file_handler, bufsize

cmd: str类型,’STOR ’ + file_name, file_name为上传后保存在服务器的文件名

file_handler: type类型,常为open()函数以‘rb’方式打开的一个文件后的方法,传入后会在内部进行调用其read()函数,将数据写入cmd的文件内

bufsize: int类型,常设置1024

返回参数:

1.2.1.6 quit()方法

函数调用: ftp.quit()

函数功能:用于退出客户端连接,会想服务端发送QUIT信息

传入参数:

返回参数:

1.2.1.7 close()方法

函数调用: ftp.close()

函数功能:用于关闭客户端,不会发送信息给服务器

传入参数:

返回参数:

1.2.1.8 dir()方法

函数调用: ftp.dir()

函数功能:用于显示服务器目录下的信息,会在debug信息中显示

传入参数:

返回参数:

1.2.1.9 mkd()方法

函数调用: ftp.mkd(dir)

函数功能:用于在服务器目录下新建一个目录,make directory

传入参数: dir

dir: str类型,’.\\’ + path_name, path_name为新建的目录名

返回参数:

1.2.1.10 cwd()方法

函数调用: ftp.cwd(dir)

函数功能: 改变服务器工作目录(基于服务器设置的目录进行),change working directory

传入参数: dir

dir: str类型,’.\\’ + path_name, path_name为新建的目录名

返回参数:

1.2.1.11 pwd()方法

函数调用: ftp.pwd(dir)

函数功能: 显示当前工作目录,若在根目录则返回’/’, present working directory

传入参数:

返回参数:

1.2.1.12 rmd()方法

函数调用: ftp.rmd(dir)

函数功能: 移除指定的目录,remove directory

传入参数: dir

dir: str类型,’.\\’ + path_name, path_name为移除的目录名

返回参数:

1.2.1.13 delete()方法

函数调用: ftp.delete(file_name)

函数功能: 移除指定的文件

传入参数: file_name

file_name: str类型,为移除的文件名

返回参数:

1.2.1.14 rename()方法

函数调用: ftp.rename(from_name, to_name)

函数功能: 更改指定文件名

传入参数: from_name, to_name

from_name: str类型,为原始的文件名

to_name: str类型,为更改后的文件名

返回参数:

 

2 FTP的客户端建立过程

客户端建立步骤主要有:

(1)      设定IP和端口号(常用21), 用户名和密码,匿名为’’;

(2)      生成ftp实例,设置调试信息等级;

(3)      connect()函数通过IP和端口连接服务器;

(4)      login()函数进行登录

(5)      利用对应函数对服务器文件进行操作

(6)      quit()函数结束客户端。

Note: STOR和RETR时设置的bufsize为传输的速度。

 1 from ftplib import FTP
 2 
 3 class FTP_Client():
 4     def __init__(self):
 5         # Info for FTP client
 6         ftp_server = 127.0.0.10
 7         ftp_port = 21
 8         # user_name = ‘Customer‘
 9         # password = ‘777777‘
10         user_name = Admin
11         password = 888888
12         # user_name = ‘‘
13         # password = ‘‘
14 
15         # Create FTP
16         self.ftp = FTP()
17         # set ftp debuglevel here, default is 0
18         self.ftp.set_debuglevel(1)
19         self.ftp.connect(ftp_server, ftp_port)
20         self.ftp.login(user_name, password)
21         print(<<< Welcome info:, self.ftp.getwelcome())
22 
23     def updateFile(self):
24         bufsize = 1024
25         updateList = []
26         # Open and read client data that need to be transfer
27         file_handler = open(FTPClientFile\\testFileCopy.py, rb)
28         # srorbinary need 3 para at least,
29         # 1st is STOR+dirName, dirName is the file name that save to server,
30         # 2nd is file_handler, open and read the client file data,
31         # 3rd is bufsize.
32         self.ftp.storbinary(STOR ClientTransfer.py, file_handler, bufsize)
33 
34     def downloadFile(self):
35         bufsize = 1024
36         downList = [testFile.py, testFile.docx, testFile.zip, testFile.txt]
37         # Create an new file to store data received
38         for down in downList:
39             file_handler = open(FTPClientFile\\copy_%s % down, wb).write
40             # retrbinary need 3 para at least,
41             # 1st is RETR+dirName, dirName is target file name,
42             # 2nd is a write function, will be called inside function, open a new file in client to store file data,
43             # 3rd is bufsize.
44             self.ftp.retrbinary(RETR %s % down, file_handler, bufsize)
45 
46     def quit(self):
47         self.ftp.quit()
48 
49     # This function can get all the contains info in server path
50     def showDir(self):
51         self.ftp.dir()
52 
53     # All this below function should base on the directory set in server.
54     # Make a new directory
55     def newDir(self, dir=.\\FTPtest):
56         self.ftp.mkd(dir)
57 
58     # Change working directory
59     def changeDir(self, dir=.\\FTPtest):
60         self.ftp.cwd(dir)
61 
62     # Return current working directory(base is ‘/‘)
63     def presentDir(self):
64         self.ftp.pwd()
65 
66     # Remove certain directory
67     def removeDir(self, dir=.\\FTPtest):
68         self.ftp.rmd(dir)
69 
70     # Delete file
71     def delFile(self, fileName):
72         self.ftp.delete(fileName)
73 
74     # Rename file
75     def renameFile(self, currName=testFile.py, reName=testFileRename.txt):
76         self.ftp.rename(currName, reName)
77 
78 ftp_client = FTP_Client()
79 ftp_client.downloadFile()
80 ftp_client.quit()

Note: 在运行客户端之前,需要先运行 ftp 服务器代码。

 

相关阅读


1. ftp 服务器

 

Python的网络编程[1] -> FTP -> 使用 ftplib 建立 FTP 客户端

标签:lib   python   pcl   ide   write   基于   目录   from   实例   

原文地址:https://www.cnblogs.com/stacklike/p/8119942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!