from ftplib import *
import os,readline
import sys
class MyFtp:
ftp = FTP() #建立一个ftp对象的链接
def __init__(self, host, port=''): #构造函数初始化
self.ftp.connect(host=host,timeout=100) #连接ftp服务器
def Login(self, user, passwd): #登录函数
self.ftp.login(user=user, passwd=passwd )
print (self.ftp.welcome) #登陆成功后ftp显示欢迎信息 def DownLoadFile(self, LocalFilePath, RemoteFilePath):
file = open(LocalFilePath, 'wb')
self.ftp.retrbinary( "RETR %s" %( RemoteFilePath ), file.write ) #下载文件到本地
file.close()
return True def DownLoadFileTree(self, LocalFileDir, RemoteFileDir):
if os.path.isdir( LocalfileDir ) == False: #如果传入本地的不是目录
os.makedirs(LocalFileDir) #就在本地新建该目录
self.ftp.cwd(RemoteFileDir) #切换到远程目录
RemoteFiles = self.ftp.nlst() #把远程目录里的所有文件都传给 RemoteFiles变量
for file in RemoteFiles:
Local = os.path.join(LocalFilesDir, file )
chang = os.path.join(RemoteDir,file )
if self.IsDir(chang):
self.DownLoadFileTree(Local, chang)
else:
self.DownLoadFile( Local, chang)
self.ftp.cwd( ".." ) def IsDir(self, path):
if os.path.isdir(path) == True:
self.juge = True
else:
self.juge = False
return self.juge def UpLoadFileTree(self, LocalFileDir, RemoteFileDir):
if os.path.isdir(LocalFileDir) == False:
print( 'wrong !Please Input Dir')
if os.path.isdir(RemoteFileDir) == False:
os.makedirs(RemoteFileDir)
LocalFiles = os.listdir(LocalFileDir)
self.ftp.cwd(RemoteFileDir)
for Local in LocalFiles:
src = os.path.join( LocalFileDir, Local)
chang = os.path.join(RemoteFileDir,Local)
if os.path.isdir(src):
self.UpLoadFileTree(src,chang)
else:
self.UpLoadFile(src,chang)
self.ftp.cwd( ".." ) def UpLoadFile(self, LocalFilePath, RemoteFilePath):
if os.path.isfile( LocalFilePath ) == False:
return False
file = open(LocalFile, "rb")
self.ftp.storbinary('STOR %s'%RemoteFile, file, 4096)
file.close() ftp = myFtp('192.168.19.153') #实例化 def Login():
ftp.Login('root','root')
def Update():
print('\033[34mUping.....\033[m')
ftp.UpLoadFileTree('main', "/xxx" )
print('Done')
def DownLoad():
print('\033[34mDowning.....\033[m')
ftp.DownLoadFileTree('localDir','remoteDir')
print ('Done')
def Close():
self.ftp.quit() def Menu():
print ("""\033[;32mWelcome \033[0m\n""")
print ("\t(1) Login")
print ("\t(2) Update")
print ("\t(3) DownLoad")
print ("\t(4) close")
while True:
choices = raw_input('\033[32mChoice>>\033[m').strip()
if len(choices) == 0:continue
if choices == '':Login()
elif choices == '':Update()
elif choices == '':DownLoad()
elif choices == '':close() Menu() http://www.cnblogs.com/zhuweiblog/p/5154752.html

ftplib模块编写简单的ftp服务的更多相关文章

  1. 1 学习wcf 编写简单的WCF服务流程 并发布在IIS上

    学习笔记 学习大佬的博客 https://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049.html  写一遍加深印象 图片看不清楚的 可 ...

  2. linux服务基础之ftp服务

    ftp是一种文件传输协议,我们以redhat6.9为服务器系统,来介绍一下ftp服务器,这里我们先介绍一下ftp协议工作的原理 ftp协议可以在不同类型的计算机之间传输文件,工作流程大致为 1:客户机 ...

  3. 安装Linux Deploy和Termux之后,再安装ftp服务软件都是多余的!

    之前以为Debian 9 running via Linux Deploy或者Termux在安卓系统部署之后,一定要安装vsftpd或者pure-ftpd这些专门的ftp服务器软件,才能提供ftp服务 ...

  4. SELECTORS模块实现并发简单版FTP

    环境:windows, python 3.5功能:使用SELECTORS模块实现并发简单版FTP允许多用户并发上传下载文件 结构:ftp_client ---| bin ---| start_clie ...

  5. python3 ftplib模块连接FTP

    from ftplib import FTP_TLS import os import re class MyFtp(FTP_TLS): """继承FTP类"& ...

  6. python模块之sys和subprocess以及编写简单的主机扫描脚本

    python模块之sys和subprocess以及编写简单的主机扫描脚本 1.sys模块 sys.exit(n)  作用:执行到主程序末尾,解释器自动退出,但是如果需要中途退出程序,可以调用sys.e ...

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

    python已经默认安装了ftplib模块,用其中的FTP类可以实现FTP文件的上传下载 FTP文件上传下载 # coding:utf8 from ftplib import FTP def uplo ...

  8. node http模块搭建简单的服务和客户端

    node-http Node.js提供了http模块,用于搭建HTTP服务端和客户端. 创建Web服务器 server.js /** * node-http 服务端 */ let http = req ...

  9. Centos 7 部署FTP服务简单版

    第三方教程推荐与参考: http://blog.csdn.net/somehow1002/article/details/70232791 先安装成功了,有信心了.再进一步扩展配置. 1.安装vsft ...

随机推荐

  1. ubuntu 下使用etcd

    1.安装ETCD_VER=v3.1.0 DOWNLOAD_URL=https://github.com/coreos/etcd/releases/download curl -L ${DOWNLOAD ...

  2. bzoj 5334 数学计算

    bzoj 5334 数学计算 开始想直接模拟过程做,但模数 \(M\) 不一定为质数,若没有逆元就 \(fAKe\) 掉了. 注意到操作 \(2\) 是删除对应的操作 \(1\) ,相当于只有 \(1 ...

  3. Codeforces 633H Fibonacci-ish II【线段树】

    LINK 题目大意 给你一个序列a,Q次询问,每次询问\([l,r]\) 把\([l,r]\)的数排序去重,得到序列b,f是斐波那契数列 求\(\sum_{b=1}^{len} b_if_i\) 思路 ...

  4. WPF 程序无法触摸操作?我们一起来找原因和解决方法!

    WPF 自诞生以来就带着微软先生的傲慢.微软说 WPF 支持触摸,于是 WPF 就真的支持触摸了.对,我说的是"支持触摸",那种摸上去能点能动的:偶尔还能带点儿多指的炫酷效果.但是 ...

  5. tableview小结-初学者的问题

    初学者的问题主要集中在,下面几个问题: 一.几个函数总是不被调用:例如: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInS ...

  6. 《DSP using MATLAB》示例Example 8.4

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. Flask第一篇——URL详解

    原创 2018-02-14 孟船长 自动化测试实战 URL是Uniform Resource Locator的缩写,即统一资源定位符. 一个URL通常由一下几个部分组成: scheme://host: ...

  8. caddy quic 协议试用&& 几个问题

    备注:    caddy  具体的安装就不介绍,quic 协议也不介绍了   1. 启用协议,比较简单 /usr/local/bin/caddy -log stdout -quic -conf=/et ...

  9. centos6 查看SELinux状态 关闭SELinux

    SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.在这种访问控制体系的限制下,进程只能访问那 ...

  10. Eclipse导入工程后,XDoclet错误:Missing library: xdoclet-1.2.1.jar. Select the home directory for XDoclet

    这几天在使用Open Health Tools的OpenXDS工程,在导入Eclipse后,出现下面的错误: 遂google之,在网上找到了答案.答案网址为http://blog.v-s-f.co.u ...