1. 连接FTP server

import ftplib
ftp = ftplib.FTP(ftpserver, user, passwd)

等同于

import ftplib
ftp = ftplib.FTP()
ftp.connect(ftpserver)
ftp.login(user,passwd)

对于初始化函数FTP(),如果指定host,则自动调用connect函数,如果指定了user和passwd,则自动调用login,如果都没指定,就什么都不做,需要显示调用。

2. upload

import ftplib

def uploadToFTP(filename, targetdir, ftpserver, user="user", passwd=""):
rtd = 0
targetdir = targetdir.split(ftpserver)[-1] # remove ftpserver string from targetdir string if contain
fp = open(filename, 'rb')
ftp = ftplib.FTP(ftpserver, user, passwd) # make connection and login FTP
try:
ftp.cwd(targetdir)
except ftplib.error_perm:
print "Error: cannot upload to FTP, no such folder"
return -2
try:
ftp.storbinary('STOR %s/%s'%(targetdir,filename), fp)
except Exception, e:
traceback.print_exc()
rtd = 1
finally:
fp.close()
ftp.quit()
return rtd

3. 下载

# ftp.retrbinary(cmd, callback, blocksize=8192, rest=None)

def downloadfile()
remotepath = "/home/pub/dog.jpg";
localpath = 'f:\\test\\dog.jpg'
fp = open(localpath,'wb') #以写模式在本地打开文件 ftp.retrbinary('RETR ' + remotepath,fp.write,bufsize) #接收服务器上文件并写入本地文件 ftp.set_debuglevel(0) #关闭调试
fp.close()
ftp.quit() #退出ftp服务器

官方文档:

https://docs.python.org/3/library/ftplib.html#ftplib.FTP.transfercmd

python FTP上传和下载文件的更多相关文章

  1. ftp上传或下载文件工具类

    FtpTransferUtil.java工具类,向ftp上传或下载文件: package utils; import java.io.File; import java.io.FileOutputSt ...

  2. FTP上传和下载文件的应用

    FTP(File Transfer Protocol)协议主要用来在网络上进行文件传输.FTP通讯除了有一个默认的端口21外,还有其他端口,同城两个端口同时进行数据传输.一个是默认的端口(通常为21) ...

  3. Java 利用FTP上传,下载文件,遍历文件目录

    Java实现FTP上传下载文件的工具包有很多,这里我采用Java自带的API,实现FTP上传下载文件.另外JDK1.7以前的版本与其之后版本的API有了较大的改变了. 例如: JDK1.7之前 JDK ...

  4. 使用脚本在FTP上传、下载文件

    由于最近勒索病毒变种又一次爆发,公司内部封锁了TCP 445端口.导致原来通过文件共享的方式上传下载的计划任务无法执行.所以,我开设了FTP服务器来完成这个工作. 关于如何建立FTP服务器,请看这里 ...

  5. ftp上传与下载文件

    准备工作 服务器已经配置好ftp服务 服务器linux centos 7.4 搭建ftp服务器:https://www.cnblogs.com/mmzs/p/10601683.html 需要用到的ja ...

  6. Python——FTP上传和下载

    一.FTP对象方法说明 login(user='anonymous',passwd='', acct='') 登录 FTP 服务器,所有参数都是可选的 pwd() 获得当前工作目录 cwd(path) ...

  7. FTP上传、下载文件Demo

    前言:最近在做一个app,负责写后台接口,客户那边给了一个FTP的账号密码过来,服务器上面放了一堆的PDF文件,让我们这边自己从上面拿,项目是spriongboot的,做个记录供以后参考. 一.app ...

  8. 结合阿里云服务器,使用FTP上传和下载文件,出现的问题和解决方案

    一.FTP出现的问题 二.在网上找的方案 如果使用FileZilla默认设置连接ftp服务器的话可能会报以下错误: 错误: 无法建立数据连接:ECONNREFUSED - Connection ref ...

  9. C# FTP上传与下载文件

    public class UploadFile { string ftpServerIP; string ftpRemotePath; string ftpUserID; string ftpPass ...

随机推荐

  1. shell 与用户交互

    bash shell如何获取命令行参数(添加到命令后的数据).命令行选项(确定命令行为的英文字母).键盘输入数据? 操作命令行参数 1 读取参数 bash shell用位置参数变量(positiona ...

  2. 插入排序 & 快速排序

    2.1 插入排序: 接口定义: int insert_sort(void* data, int size, int esize, int (*compare)(const void* key1, co ...

  3. datawindow直接导入导出xml

    long dwcontrol.ImportFile ( XML!, filename ) dw_.Modify("DataWindow.Export.XML.UseTemplate = 't ...

  4. Integer对象

    数字格式的字符串转成基本数据类型的方法: 1:将该字符串封装成了Integer对象,并调用对象的方法intValue(); 2:使用Integer.parseInt(numstring):不用建立对象 ...

  5. C语言中结构体的初始化

    直接上例子: struct point { int x; int y; int z; } //常规写法 struct point pt1 = {100, 300, 200}; //初始化个数少于实际个 ...

  6. 结对开发----找出“水王"

    一.题目 三人行设计了一个灌水论坛.信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子.坊间风闻该“水王”发帖数目超过了帖子数目的一半. 如果 ...

  7. JS 面向对象之继承 -- 原型链

    ECMAScript只支持实现继承,其实现继承主要是靠原型链来实现. 原型链的基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法. 简单回顾下构造函数.原型和实例的关系: 每个构造函数都有 ...

  8. WebSphere数据源配置

    WebSphere data source Configuration login http://localhost:9061/ibm/console/login.do(According to yo ...

  9. Solr Schema.xml和solrconfig.xml分析(转)

    Solr Schema.xml和solrconfig.xml分析 (http://yinwufeng.iteye.com/blog/964040) 一.字段配置(schema) schema.xml位 ...

  10. HDU1070Milk

     Milk Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...