2.4 利用FTP服务器下载和上传目录
利用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服务器下载和上传目录的更多相关文章
- 2.3 利用FTP服务器下载和上传文件
二.利用FTP服务器的下载文件 from ftplib import FTP from os.path import exists def getfile(file,site,dir,user=(), ...
- FTP服务器文件上传的代码实现
方式一: @Test public void testFtpClient() throws Exception { // 1.创建一个FtpClient对象 FTPClient ftpClient = ...
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传
[源码下载] 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 后台 ...
- 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性
[源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...
- (4)FTP服务器下载文件
上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...
- github下载和上传项目
git下载和上传项目 下载: git clone +地址 上传: 1.git init 在当前项目的目录中生成本地的git管理(多一个.git文件夹,为隐藏文件) 2.git add .(注意最后面有 ...
- [CentOs7]搭建ftp服务器(3)——上传,下载,删除,重命名,新建文件夹
摘要 上篇文章介绍了如何为ftp添加虚拟用户,本篇将继续实践如何上传,下载文件. 上传 使用xftp客户端上传文件,如图所示 此时上传状态报错,查看详情 从错误看出是应为无法创建文件造成的.那么我们就 ...
- 使用递归方法实现,向FTP服务器上传整个目录结构、从FTP服务器下载整个目录到本地的功能
我最近由于在做一个关于FTP文件上传和下载的功能时候,发现Apache FTP jar包没有提供对整个目录结构的上传和下载功能,只能非目录类型的文件进行上传和下载操作,后来我查阅很多网上的实现方法,再 ...
随机推荐
- JAVA 多线程之volatile的介绍
volatile的介绍 volatile的主要作用是:提示编译器该对象的值有可能在编译器未监测的情况下被改变. volatile类似于大家所熟知的const也是一个类型修饰符.volatile是给编译 ...
- Flask最强攻略 - 跟DragonFire学Flask - 第十五篇 Flask-Script
其实本章就是为下一章做的铺垫啦,但是也要认真学习哦 Flask-Script 从字面意思上来看就是 Flask 的脚本 是的,熟悉Django的同学是否还记得Django的启动命令呢? python ...
- Hdu1241 Oil Deposits (DFS)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- 简单的sql server->bs或cs数据交互模式
主要记录工作当中遇到的一些问题和总结的一些经验 客户端请求-->web服务接口-->sql 语句执行(存储在数据库中)-->web服务(客户端通过调用web服务接口)-->返回 ...
- sqlserver group by后获取其他字段(多种方法)
大家都知道用group by的话,select 后面指定的字段必须与group by后面的一致.group by 只有个别字段,如果拿出其他未分组的字段信息呢?在网上搜了下, 总结如下: 使用了gro ...
- Python3 pip命令报错:Fatal error in launcher: Unable to create process using '"'
Python3 pip命令报错:Fatal error in launcher: Unable to create process using '"' 一.问题 环境:win7 同时安装py ...
- Docker Kubernetes Volume 本地数据卷
Docker Kubernetes Volume 本地数据卷 emptyDir 当Pod分配到Node时,首先创建一个空卷,并挂载到Pod中的容器. Pod中的容器可以读取和写入卷中的文件. 当Pod ...
- 剑指offer(7)斐波那契数列
题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 题目分析 我们都知道斐波那契可以用递归,但是递归重复计算的部分太多了(虽然可以通过),但是这 ...
- 一.rest-framework之版本控制 二、Django缓存 三、跨域问题 四、drf分页器 五、响应器 六、url控制器
一.rest-framework之版本控制 1.作用 用于版本的控制 2.内置的版本控制 from rest_framework.versioning import QueryParameterVer ...
- socket实现文件传输
server:===========================================import socketimport structimport jsonsk = socket.s ...