# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之模块ftplib(FTP协议的客户端) #需求:快速进行ftp上传 ,下载,查询文件 from ftplib import FTP
ftp = FTP() #设置变量 timeout = 30
port = 21 ftp.connect('192.168.1.188',port,timeout) # 连接FTP服务器
ftp.login('UserName','') # 登录 print ftp.getwelcome() # 获得欢迎信息 ftp.cwd('file/test') # 设置FTP远程目录(路径)
list = ftp.nlst() # 获取目录下的文件,获得目录列表
for name in list:
printname
path = 'd:/data/' + name # 定义文件保存路径
f = open(path,'wb') # 打开要保存文件
filename = 'RETR ' + name # 保存FTP文件
ftp.retrbinary(filename,f.write) # 保存FTP上的文件
ftp.delete(name) # 删除FTP文件
ftp.storbinary('STOR '+filename, open(path, 'rb')) # 上传FTP文件
ftp.quit() '''
Example:
>>> from ftplib import FTP
>>> ftp = FTP('ftp.python.org') #连接ftp服务器;connect to host, default port
>>> ftp.login() # default, i.e.: user anonymous, passwd anonymous@
'230 Guest login ok, access restrictions apply.'
>>> ftp.retrlines('LIST') # list directory contents
total 9
drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .
drwxr-xr-x 8 root wheel 1024 Jan 3 1994 ..
drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin
drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc
d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming
drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib
drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub
drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr
-rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg
'226 Transfer complete.'
>>> ftp.quit() #断开服务器连接
'221 Goodbye.' class FTP
ftp=FTP() #设置变量,类似于初始化
| An FTP client class. | Methods:
| acct(self, password)
| Send new account name.
|
| close(self) #close连接
| Close the connection without assuming anything about it.
|
| connect(self, host='', port=0, timeout=-999) #连接的ftp sever和端口,如:ftp.connect('192.168.1.188',21,30)
|
| cwd(self, dirname) #把当前目录设置为path,设置FTP当前操作的路径
| Change to a directory.
|
| debug = set_debuglevel(self, level) #ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
|
| delete(self, filename) #删除远程文件
| Delete a file.
|
| dir(self, *args) #显示目录下文件信息
| List a directory in long form.
| By default list current directory to stdout.
| Optional last argument is callback function; all
| non-empty arguments before it are concatenated to the
| LIST command. (This *should* only be used for a pathname.)
|
| getline(self) #从服务器输出一行数据
| # Internal: return one line from the server, stripping CRLF.
| # Raise EOFError if the connection is closed
|
| getmultiline(self)
| # Internal: get a response from the server, which may possibly
| # consist of multiple lines. Return a single string with no
| # trailing CRLF. If the response consists of multiple lines,
| # these are separated by '\n' characters in the string
|
| getresp(self)
| # Internal: get a response from the server.
| # Raise various errors if the response indicates an error
|
| getwelcome(self) #打印出欢迎信息
| Get the welcome message from the server.
| (this is read and squirreled away by connect())
|
| login(self, user='', passwd='', acct='') #登录到FTP服务器,所有的参数都是可选的.
| Login, default anonymous.
|
| makepasv(self)
|
| makeport(self) #创建一个新的套接字,并发送一个端口命令。
| Create a new socket and send a PORT command for it.
|
| mkd(self, dirname) #创建远程目录;建立一个目录,返回其完整路径名
| Make a directory, return its full pathname.
|
| nlst(self, *args) #与dir()类似,但返回一个文件名的列表,而不是显示这些文件名
#返回给定目录下的文件列表(默认情况下)
| Return a list of files in a given directory (default the current).
|
| ntransfercmd(self, cmd, rest=None)
| Initiate a transfer over the data connection.
|
| If the transfer is active, send a port command and the
| transfer command, and accept the connection. If the server is
| passive, send a pasv command, connect to it, and start the
| transfer command. Either way, return the socket for the
| connection and the expected size of the transfer. The
| expected size may be None if it could not be determined.
|
| Optional `rest' argument can be a string that is sent as the
| argument to a REST command. This is essentially a server
| marker used to tell the server to skip over any data up to the
| given marker.
|
| putcmd(self, line)
| # Internal: send one command to the server (through putline())
|
| putline(self, line)
| # Internal: send one line to the server, appending CRLF
|
| pwd(self) #当前工作目录
| Return current working directory.
|
| quit(self) #退出ftp
| Quit, and close the connection.
|
| rename(self, fromname, toname) #改文件名,把远程文件fromname 改名为toname
| Rename a file.
|
| retrbinary(self, cmd, callback, blocksize=8192, rest=None)
#与retrlines()类似,只是这个指令处理二进制文件。回调函数
| Retrieve data in binary mode. A new port is created for you.
|
| Args:
| cmd: A RETR command.
| callback: A single parameter callable to be called on each
| block of data read.
| blocksize: The maximum number of bytes to read from the
| socket at one time. [default: 8192]
| rest: Passed to transfercmd(). [default: None]
|
| Returns:
| The response code.
|
| retrlines(self,cmd,callback=None) #
#ftp.retrlines('LIST')#返回目录内容
#此时可以获得当前ftp目录下的所有文件的信息
| Retrieve data in line mode. A new port is created for you.
|
| Args:
| cmd: A RETR, LIST, NLST, or MLSD command.
| callback: An optional single parameter callable that is called
| for each line with the trailing CRLF stripped.
| [default: print_line()]
|
| Returns:
| The response code.
|
| rmd(self, dirname) #删除远程目录
| Remove a directory.
|
| sanitize(self, s)
| # Internal: "sanitize" a string for printing
|
| sendcmd(self, cmd)
| Send a command and return the response.
|
| sendeprt(self, host, port)
| Send a EPRT command with the current host and the given port number.
|
| sendport(self, host, port)
| Send a PORT command with the current host and the given
| port number.
|
| set_debuglevel(self, level)
#ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
#ftp.set_debuglevel(0) #关闭调试模式
| Set the debugging level.
| The required argument level means:
| 0: no debugging output (default)
| 1: print commands and responses but not body text etc.
| 2: also print raw lines read and sent before stripping CR/LF
|
| set_pasv(self, val)
| Use passive or active mode for data transfers.
| With a false argument, use the normal PORT mode,
| With a true argument, use the PASV command.
|
| size(self, filename) #检索文件大小
| Retrieve the size of a file.
|
| storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None)
#上传FTP文件
#ftp.storbinaly("STOR filename.txt",file_handel,bufsize) #上传目标文件
#ftp.storbinary('STOR '+filename, open(path, 'rb')) # 上传FTP文件
#注意storlines的解释
#只是这个指令处理二进制文件。要给定一个文件对象f,上传块大小bs 默认为8Kbs=8192])
| Store a file in binary mode. A new port is created for you.
|
| Args:
| cmd: A STOR command.
| fp: A file-like object with a read(num_bytes) method.
| blocksize: The maximum data size to read from fp and send over
| the connection at once. [default: 8192]
| callback: An optional single parameter callable that is called on
| each block of data after it is sent. [default: None]
| rest: Passed to transfercmd(). [default: None]
|
| Returns:
| The response code.
|
| storlines(self, cmd, fp, callback=None)
#storlines(cmd, f)
#给定FTP 命令(如“STOR filename”),以上传文本文件。要给定一个文件对象f
| Store a file in line mode. A new port is created for you.
|
| Args:
| cmd: A STOR command.
| fp: A file-like object with a readline() method.
| callback: An optional single parameter callable that is called on
| each line after it is sent. [default: None]
|
| Returns:
| The response code.
|
| transfercmd(self, cmd, rest=None)
| Like ntransfercmd() but returns only the socket.
|
| voidcmd(self, cmd)
| Send a command and expect a response beginning with '2'.
|
| voidresp(self)
| Expect a response beginning with '2'. DATA
__all__ = ['FTP', 'Netrc', 'FTP_TLS']
'''

python之模块ftplib(FTP协议的客户端)的更多相关文章

  1. python之模块ftplib(实现ftp上传下载代码)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ftplib(实现ftp上传下载代码) #需求:实现ftp上传下载代码(不含错误处理) f ...

  2. FTP协议简介

    1. FTP协议概述 FTP协议的英文全称为File Transfer Protocol, 简称为FTP, 它是从一个主机向一个主机传输文件的协议. FTP协议中客户端和服务器进行文件交互的方式如下图 ...

  3. Python的网络编程[1] -> FTP 协议[2] -> 使用 ftplib 建立 FTP 客户端

    使用 ftplib 建立 FTP 客户端 用于建立FTP Client,与 pyftplib 建立的 Server 进行通信. 快速导航 1. 模块信息 2. 建立 FTP 客户端 1. 模块信息 1 ...

  4. Python ftplib 模块关于 ftp的下载

    import ftplib import os import socket import sys HOST='192.168.216.193' DIRN='c:\\ftp\FTP.123' FILE= ...

  5. Python脚本暴力破解FTP口令(ftplib)

    目录 判断FTP服务器是否允许匿名登录 暴力破解FTP口令 列出FTP目录内的网页文件 综合 环境:Windows python2.7.15 ftplib模块是python下用于ftp服务的模块 . ...

  6. python模块:网络协议和支持

    python模块:网络协议和支持 webbrowser 调用浏览器显示html文件 webbrowser.open('map.html') [webbrowser - Convenient Web-b ...

  7. FTP文件传输协议两种模式 ftp协议集,错误码集,ftp客户端命令集

    TCP/IP协议中,FTP标准命令TCP端口号为21,Port方式数据端口为20.FTP协议的任务是从一台计算机将文件传送到另一台计算机,它与这两台计算机所处的位置.联接的方式.甚至是是否使用相同的操 ...

  8. Python的网络编程[1] -> FTP 协议[0] -> FTP 的基本理论

    FTP协议 / FTP Protocol FTP全称为File Transfer Protocol(文件传输协议),常用于Internet上控制文件的双向传输,常用的操作有上传和下载.基于TCP/IP ...

  9. Python脚本通过ftp协议移植文件

    需求 项目需要定时移植多个客户服务器的文件到公司服务器上,确保文件定时同步和生成监控日志 机制原理 1.客户和公司服务器同时安装vpn,绕过复杂的网关,linux下使用的OpenVPN 2.服务器定时 ...

随机推荐

  1. npm ERR! Error extracting ~/.npm/cloudant/1.9.0/package.tgz archive: ENOENT: no such file or directory, open '~/.npm/cloudant/1.9.0/package.tgz'

    修改package.json Thanks machines returning the above error when , just and now all the builds are pass ...

  2. [转]Mysql中的SQL优化与执行计划

    From : http://religiose.iteye.com/blog/1685537 一,如何判断SQL的执行效率? 通过explain 关键字分析效率低的SQL执行计划. 比如: expla ...

  3. noip 1998 洛谷P1013 进制位

    题目描述 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字. 例如: L K V E L L K V E K K V E KL V V E KL KK E E K ...

  4. Chart/Report资源目录

    ylbtech-Chart:Chart/Report资源目录 1.Chart.js返回顶部 1-0.官网 http://www.chartjs.org 1-1.实例 http://www.chartj ...

  5. 基于ZigBee和STM32的智能家居控制系统的设计与实现(三)

    基于ZigBee和STM32的智能家居控制系统的设计与实现(三) 自从前两篇博客介绍了智能家居系统的基本实现机理后,收到了好多朋友的来信,和我讨论了好多的这方面的知识,在此很高兴,虽然自己做的这个所谓 ...

  6. libcurl HTTP POST请求向服务器发送json数据【转】

    转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...

  7. [leetcode]Spiral Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix/ 题意: Given a matrix of m x n elements (m rows, n ...

  8. [leetcode]Subsets @ Python

    原题地址:https://oj.leetcode.com/problems/subsets/ 题意:枚举所有子集. 解题思路:碰到这种问题,一律dfs. 代码: class Solution: # @ ...

  9. 梯度消失(vanishing gradient)与梯度爆炸(exploding gradient)问题

    (1)梯度不稳定问题: 什么是梯度不稳定问题:深度神经网络中的梯度不稳定性,前面层中的梯度或会消失,或会爆炸. 原因:前面层上的梯度是来自于后面层上梯度的乘乘积.当存在过多的层次时,就出现了内在本质上 ...

  10. 4.3 使用 SQL 语句操作数据框

    下载并安装 “sqldf” 包 library(sqldf) newData <- sqldf("select * from mtcars where carb=1 order by ...