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包没有提供对整个目录结构的上传和下载功能,只能非目录类型的文件进行上传和下载操作,后来我查阅很多网上的实现方法,再 ...
随机推荐
- 第三章 document对象及数组
1.数组的使用(1)声明数组var 数组名=new Array();(2)数组赋值数组名[下标]=值: 2.数组声明,分配空间,赋值同时进行var 数组名=new Array(值1,值2....)va ...
- 16款优秀的Vue UI组件库推荐
16款优秀的Vue UI组件库推荐 Vue 是一个轻巧.高性能.可组件化的MVVM库,API简洁明了,上手快.从Vue推出以来,得到众多Web开发者的认可.在公司的Web前端项目开发中,多个项目采用基 ...
- Oarcle 入门之where关键字
--where 关键字 --作用:过滤行 *将需要的行用where引导出来 用法: 1.判断符号:=,!=,<>,<,>,<=,>=,any,some,all; 例 ...
- (转) SpringMVC学习笔记-
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--conf ...
- GoldenGate实时投递数据到大数据平台(7)– Apache Hbase
Apache Hbase安装及运行 安装hbase1.4,确保在这之前hadoop是正常运行的.设置相应的环境变量, export HADOOP_HOME=/u01/hadoop export HBA ...
- js生成的cookie在yii2中获取不到的解决办法
在js中创建的cookie,默认用yii2中自带的方法Yii::$app->request->cookies->get('abc')获取不到,而用$_COOKIE['abc']又是能 ...
- [c/c++] programming之路(12)、循环结构
一.求2n #include<stdio.h> void main(){ ; ; while(n--){ s*=; printf("%d,%d\n",s,n); } g ...
- BBU和RRU具体区别是 什么?
RRU是Remote Radio Unit 远端射频模块,BBU是Building Baseband Unit 室内基带处理单元.基带BBU集中放置在机房,RRU可安装至楼层,BBU与RRU之间采用光 ...
- ARM基础
ARM汇编:(APCS过程调用标准) 汇编:用助记符(如$ # .)代替操作码,用地址符号或标签代替地址码的编程语言 特点: 优点:可以直接访问硬件,目标代码简短,执行速度快(CPU启动时需要直接操作 ...
- java基础hashmap
Iterator中hasNext(), next() 在Iterator类中,我们经常用到两个方法: hasNext(), next(),具体含义: next(), 是返回当前元素, 并指向下一个元 ...