利用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. CentOS 7 nginx+tomcat9 session处理方案之session保持

    Session保持(会话保持)是我们见到最多的名词之一,通过会话保持,负载均衡进行请求分发的时候保证每个客户端固定的访问到后端的同一台应用服务器.会话保持方案在所有的负载均衡都有对应的实现.而且这是在 ...

  2. 152. Maximum Product Subarray(动态规划)

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  3. easy ui datatimebox databox 当前时间

    databox  当前日期: class="easyui-datebox" var curr_time = new Date(); var strDate = curr_time. ...

  4. 第八篇——Struts2的处理结果类型

    Struts2处理结果类型 1.SUCCESS:表示Action正确的执行完成,返回相应的视图,success是name属性的默认值: 2.ERROR:表示Action执行失败,返回到错误处理视图: ...

  5. 使用日期插件用js处理日期格式

    function compareDate(checkStartDate, checkEndDate) {    var arys1= new Array();    var arys2= new Ar ...

  6. 配置cron定时任务

    题:配置一个 cron 任务用户 natasha 必须配置一个定时执行任务,每天在本地时间 14:23 时执行命令* /bin/echo hiya 答: # 方法1 # su - natasha # ...

  7. 剑指offer(29)最小的K个数

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 题目分析 这题有两种方法来做. 第一种就是基于partition的 ...

  8. Linux系统组成

     1 系统组成 BootLoader:操作系统引导程序 内核: 文件系统:应用程序(用户开发的.网上下载的) 2 安装USB驱动 dongry@d-linux:~$ insmod usb_dnw.ko ...

  9. 622 CircularQueue C#

    public class MyCircularQueue { int[] Queue=null; int _Front = 0; int _Rear = 0; int Length = 0; int ...

  10. Docker入门 配置篇

    docker配置 http://www.runoob.com/docker/docker-tutorial.html