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. python函数作用域

    python中函数作用域 在python中,一个函数就是一个作用域 name = 'xiaoyafei' def change_name(): name = '肖亚飞' print('在change_ ...

  2. Graham扫描法

    Graham扫描法求凸包的模板 运行之后可以得到存有凸包顶点的栈s和栈顶指针top,n代表总点数 这个模板我当时调了很久,主要难点有两个,一个是正确的极角排序,一个是出栈入栈的细节操作,逆时针扫描,这 ...

  3. PXE_kickstart安装Ubuntu

    1.准备 1.1.apt-get remove iptables卸载IP过滤表1.2.下载镜像 2.DHCP安装2.1.apt-get install dhcp3-server(在ubuntu12中, ...

  4. 如何组织一个同时面向 UWP/WPF/.Net Core 控制台的 C# 项目解决方案

    希望写一个小型工具,给自己和需要的人.考虑到代码尽可能的复用,我准备采用 .Net Standard 来编写大多数核心代码,并基于 .Net Core 编写跨平台控制台入口,用 WPF 编写桌面端 U ...

  5. EXCEL某列长度超过255个字符导入SQL SERVER2005的处理方法

    将注册表中 Jet引擎. HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Jet/4.0/Engines/Excel 如果是:ACE引擎. HKEY_LOCAL_MACHI ...

  6. Thinkphp 下 MySQL group by 接count 获得条数方法

    比如 下面的语句 , 用于分组统计 select count(*) from es_diabetes where uid=43658 GROUP BY uniques 结果明显不是我们想要得,为什么呢 ...

  7. Appium定位 5 ——xpath

    今天我们先来和大家说一下appium,上一次说到了定位,不知道大家定位的怎么样了,今天来说如何定位xpath,五分钟即可学会: 例1: 现在我想定位下面这个登录按钮: xpath该怎么写呢? 先不管三 ...

  8. 接口测试框架——第一篇-大框架和setting.py常量文件

    基础知识已经准备的差不多了,今天开始我们就开始写我们的接口测试框架,框架结构已经说过了: 今天我们先完善需要的常量,也就是setting.py文件中的内容,代码如下: # coding: utf-8 ...

  9. svn 操作命令

    1.第一次提交代码到svn svn import project_directory PATH 2.将文件checkout到本地svn checkout path(path是服务器上的目录) 例如:s ...

  10. tomcat  nginx  证书切换

    1. 导出公钥 keytool -export -alias tomcat -keystore <you jks>wsriakey.keystore -file <outputfil ...