python ftplib模块编写简单的ftp服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python ftplib模块编写简单的ftp服务相关的知识,希望对你有一定的参考价值。

 1 from ftplib import *
 2 import os,readline
 3 import sys
 4 class MyFtp:
 5      ftp = FTP()   #建立一个ftp对象的链接
 6      def __init__(self, host, port=21):         #构造函数初始化
 7          self.ftp.connect(host=host,timeout=100)  #连接ftp服务器
 8      def Login(self, user, passwd):               #登录函数
 9          self.ftp.login(user=user, passwd=passwd ) 
10          print (self.ftp.welcome)                  #登陆成功后ftp显示欢迎信息
11          
12          def DownLoadFile(self, LocalFilePath, RemoteFilePath):
13          file = open(LocalFilePath, wb)
14          self.ftp.retrbinary( "RETR %s" %( RemoteFilePath ), file.write )  #下载文件到本地
15          file.close()
16          return True
17  
18      def DownLoadFileTree(self, LocalFileDir, RemoteFileDir):
19          if os.path.isdir( LocalfileDir ) == False:     #如果传入本地的不是目录
20              os.makedirs(LocalFileDir)                   #就在本地新建该目录
21          self.ftp.cwd(RemoteFileDir)                     #切换到远程目录
22          RemoteFiles = self.ftp.nlst()                   #把远程目录里的所有文件都传给 RemoteFiles变量
23          for file in RemoteFiles:
24              Local = os.path.join(LocalFilesDir, file )
25              chang = os.path.join(RemoteDir,file )
26              if self.IsDir(chang):
27                  self.DownLoadFileTree(Local, chang)
28              else:
29                  self.DownLoadFile( Local, chang)
30          self.ftp.cwd( ".." )
31  
32      def IsDir(self, path):
33          if os.path.isdir(path) == True:
34             self.juge = True
35          else:
36              self.juge = False
37          return self.juge
38                  
39      def UpLoadFileTree(self, LocalFileDir, RemoteFileDir):
40          if os.path.isdir(LocalFileDir) == False:
41              print( wrong !Please Input Dir)
42          if os.path.isdir(RemoteFileDir) == False:
43              os.makedirs(RemoteFileDir)
44          LocalFiles = os.listdir(LocalFileDir)
45          self.ftp.cwd(RemoteFileDir)
46          for Local in LocalFiles:
47             src = os.path.join( LocalFileDir, Local)
48             chang = os.path.join(RemoteFileDir,Local)
49             if os.path.isdir(src):
50              self.UpLoadFileTree(src,chang)
51             else:
52              self.UpLoadFile(src,chang)
53         self.ftp.cwd( ".." )
54                 
55      def UpLoadFile(self, LocalFilePath, RemoteFilePath):
56          if os.path.isfile( LocalFilePath ) == False:
57              return False
58          file = open(LocalFile, "rb")
59          self.ftp.storbinary(STOR %s%RemoteFile, file, 4096)
60          file.close()
61  
62  
63 ftp = myFtp(192.168.19.153)   #实例化
64 
65 def Login():
66     ftp.Login(root,root)        
67 def Update():
68     print(\033[34mUping.....\033[m)
69     ftp.UpLoadFileTree(main, "/xxx" )
70     print(Done)
71 def DownLoad():
72     print(\033[34mDowning.....\033[m)
73     ftp.DownLoadFileTree(localDir,remoteDir)
74         print (Done)
75 def Close():
76      self.ftp.quit()
77 
78 def Menu():
79     print ("""\033[;32mWelcome \033[0m\n""")
80     print ("\t(1) Login")
81     print ("\t(2) Update")
82     print ("\t(3) DownLoad")
83     print ("\t(4) close")
84     while True:
85        choices = raw_input(\033[32mChoice>>\033[m).strip()
86        if len(choices) == 0:continue
87        if choices   == 1:Login()
88        elif choices == 2:Update()
89        elif choices == 3:DownLoad()
90        elif choices == 4:close()
91        
92 Menu()



以上是关于python ftplib模块编写简单的ftp服务的主要内容,如果未能解决你的问题,请参考以下文章

python:使用ftplib编写FTP客户端

python ftplib模块

ftplib模块

Python之ftplib模块

Python ftplib模块

python使用ftplib模块实现FTP文件的上传下载