利用FTP服务器下载目录

import os,sys
from ftplib import FTP
from mimetypes import guess_type nonpassive = False #passive FTP by default
remotesite = '192.168.191.1'
remotedir = '.' #FTP的路径
remoteuser = () #因为我没设置密码,所以为空集 localdir = '.' #本地路径 clean_all = input( 'Clean local directory first? ')[:1] in ['y','Y'] #是否清除本地目录所有文件
#连接PFTP
print('connecting...')
connection = FTP(remotesite)
connection.login(*remoteuser)
connection.cwd(remotedir)
if nonpassive:
connection.set_pasv(False) #most servers do passive
#清除
if clean_all:
for localname in os.listdir(localdir):
try:
print('deleting local',localname)
os.remove(os.path.join(remotedir,localname))
except:
print('cannot delete', localname) count = 0
remotefiles = connection.nlst()
#只能下载目录中的文件,不能下载目录中的目录
for remotename in remotefiles[:5]:
if remotename in ('.','..') or not '.' in remotename:continue #判断是否目录,这里根据实际情况更改
mimetype,encoding = guess_type(remotename)
mimetype = mimetype or '?/?'
mimetype = mimetype.split('/')[0] localpath = os.path.join(localdir,remotename)
print('downing',remotename,'to',localpath,end=' ')
print('as',mimetype,encoding or '')
#保存文件
if mimetype == 'text' and encoding == None:
localfile = open(localpath,'w',encoding=connection.encoding)
callback = lambda line: localfile.write(line + '\n')
connection.retrlines('RETR '+remotename,callback)
else:
localfile = open(localpath,'wb')
connection.retrbinary('RETR '+remotename,localfile.write) localfile.close()
count += 1 connection.quit()
print('Done:',count,'file download.')

利用FTP服务器上传目录

import os,sys
from ftplib import FTP
from mimetypes import guess_type nonpassive = False #passive FTP by default
remotesite = '192.168.191.1'
remotedir = 'RRR' #FTP的路径
remoteuser = () #因为我没设置密码,所以为空集 localdir = 'TTT' #本地路径 clean_all = input( 'Clean local directory first? ')[:1] in ['y','Y'] #是否清除远程目录所有文件
#连接PFTP
print('connecting...')
connection = FTP(remotesite)
connection.login(*remoteuser)
connection.cwd(remotedir)
if nonpassive:
connection.set_pasv(False) #most servers do passive
#清除
if clean_all:
for remotename in connection.nlst():
try:
print('deleting local',remotename)
connection.delete(remotename)
except:
print('cannot delete', remotename) count = 0
localfiles = os.listdir(localdir)
#只能下载目录中的文件,不能下载目录中的目录
for localname in localfiles[:5]:
mimetype,encoding = guess_type(localname)
mimetype = mimetype or '?/?'
mimetype = mimetype.split('/')[0] localpath = os.path.join(localdir,localname)
print('downing',localname,'to',localpath,end=' ')
print('as',mimetype,encoding or '')
#保存文件
if mimetype == 'text' and encoding == None:
localfile = open(localpath,'rb')
connection.storlines('RETR '+localname,localfile)
else:
localfile = open(localpath,'rb')
connection.storbinary('RETR '+localname,localfile) localfile.close()
count += 1 connection.quit()
print('Done:',count,'file uploaded.')

2.4 利用FTP服务器下载和上传目录的更多相关文章

  1. 2.3 利用FTP服务器下载和上传文件

    二.利用FTP服务器的下载文件 from ftplib import FTP from os.path import exists def getfile(file,site,dir,user=(), ...

  2. FTP服务器文件上传的代码实现

    方式一: @Test public void testFtpClient() throws Exception { // 1.创建一个FtpClient对象 FTPClient ftpClient = ...

  3. 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)

    如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...

  4. 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传

    [源码下载] 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 后台 ...

  5. 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...

  6. (4)FTP服务器下载文件

    上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...

  7. github下载和上传项目

    git下载和上传项目 下载: git clone +地址 上传: 1.git init 在当前项目的目录中生成本地的git管理(多一个.git文件夹,为隐藏文件) 2.git add .(注意最后面有 ...

  8. [CentOs7]搭建ftp服务器(3)——上传,下载,删除,重命名,新建文件夹

    摘要 上篇文章介绍了如何为ftp添加虚拟用户,本篇将继续实践如何上传,下载文件. 上传 使用xftp客户端上传文件,如图所示 此时上传状态报错,查看详情 从错误看出是应为无法创建文件造成的.那么我们就 ...

  9. 使用递归方法实现,向FTP服务器上传整个目录结构、从FTP服务器下载整个目录到本地的功能

    我最近由于在做一个关于FTP文件上传和下载的功能时候,发现Apache FTP jar包没有提供对整个目录结构的上传和下载功能,只能非目录类型的文件进行上传和下载操作,后来我查阅很多网上的实现方法,再 ...

随机推荐

  1. 记账本微信小程序开发六

    记账本微信小程序开发六 我的界面 主界面

  2. springboot之jar包部署步骤

    eclipse中: 1.单击整个项目 run as - maven clean - maven install 2.找到项目所在的路径 找到所有的jar包 3.把jar包放到linux对应的文件夹 l ...

  3. js 星星效果思路

    //星星的效果思路 1.获取需要修改的元素 ul li 跟p 布局 2.给li 加移入事件 更改提示框显示, 3.给li 加移出事件 更改提示框隐藏 4.给li加索引值代表自己的序号 5.在li移入时 ...

  4. Collections.sort 的日期排序

    public static void main(String[] args) throws ParseException { // sort降序排列 List<Date> dates = ...

  5. 面试常问Spring IOC,不得不会。

    广义的 IOC IoC(Inversion of Control) 控制反转,即“不用打电话过来,我们会打给你”. 两种实现: 依赖查找(DL)和依赖注入(DI). IOC 和 DI .DL 的关系( ...

  6. 性能测试监控工具nmon详解和分析

    性能测试监控工具nmon详解和分析 1.命令安装 1.查看liunx版本版本x86_64_14i 目录:cd /nmon/logs/ 版本x86_64_14i [root@localhost u06] ...

  7. tomcat下面web应用发布路径配置 ( 即虚拟目录配置 )

    https://blog.csdn.net/AnQ17/article/details/52122236

  8. 【HNOI 2018】游戏

    Problem Description 一次小 \(G\) 和小 \(H\) 在玩寻宝游戏,有 \(n\) 个房间排成一列,编号为 \(1,2,-,n\),相邻房间之间都有 \(1\) 道门.其中一部 ...

  9. Webpack + vue 搭建

    前言: 为何使用webpack? 为何相对于gulp&grunt更有优势 WebPack(前往官网)可以看做是模块打包机:直接分析项目结构,找到JavaScript模块以及其它的一些浏览器不能 ...

  10. poj 3304 Segments 线段与直线相交

    Segments Time Limit: 1000MS   Memory Limit: 65536K       Description Given n segments in the two dim ...