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()

http://www.cnblogs.com/zhuweiblog/p/5154752.html

 

转载于:https://www.cnblogs.com/tkinter/p/5629595.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值