python之模块ftplib(FTP协议的客户端)

# -*- 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‘,‘888888‘)                  # 登录

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 [email protected]
    ‘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‘]
‘‘‘
时间: 2024-12-11 11:06:03

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

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

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ftplib(实现ftp上传下载代码) #需求:实现ftp上传下载代码(不含错误处理) from ftplib import FTP def ftpconnect(): ftp_server='ftp.python.org' ftp=FTP() ftp.set_debuglevel(2)#打开调式级别2 ftp.connect(ftp_server,21) ftp.login('',''

Python socket模块实现TCP服务端客户端

Python socket模块实现TCP服务端客户端 写了详细的注释,如果有哪一行不明白,可留言哦. 服务端脚本 # _*_ coding: utf-8 _*_ __author__ = 'xiaoke' __date__ = '2018/6/13 14:39' # 这个脚本创建一个TCP服务器,它接收来自客户端的消息,然后将消息加上时间戳前缀并返回客户端 import socket from time import ctime HOST = '' PORT = 21567 BUFSIZ = 4

python socket模块:TCP,UDP客户端

__author__ = 'Administrator' import socket # tcp客户端 target_host = 'www.51cto.com' target_port = 80 # AF_INET是标准IPv4地址或主机名,SOCK_STREAM表示是TCP的方式 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立连接 client.connect((target_host, target_port)

Python ftplib 模块关于 ftp的下载

import ftplib import os import socket import sys HOST='192.168.216.193' DIRN='c:\\ftp\FTP.123' FILE='FTP.123' USER_NAME='123' PWD='123' def Downloadfile(file_name): try: f=ftplib.FTP(HOST) except(sockt.error, socket.gaierror) as e: print ('error: can

python网络编程socket模块实现ftp上传下载

本实验实现ftp上传文件下载文件功能,并具有校验文件完整性,打印进度条功能, 主要练习socket,struct模块. ftp用户文件存放在user.json文件中 user.json文件内容 {"lisi": "abcdef", "hyh": "123456"} ftp客户端脚本ftpclient.py #!/usr/bin/python # --*-- coding: utf-8 --*-- import socket i

Python下使用ftplib上传文件到ftp上

生产情况:tomcat下业务log备份,目录分多级,然后对应目录格式放到ftp上:所以,结构上 我就是一级一级目录进行判断(因为我没有找到在ftp一次判断其子目录是否存在),还有一个low点就是我没有找到怎样一次性的调用ftp的login因为现在每次判断都需要登录一下,最终功能是实现了:想着先贴出来 #!/usr/local/bin/python3.5 ###Description: 上传业务log到FTP199 ###Author: Tonny.Deng ###DateTime: 2016-1

各种 Python 库/模块/工具

1 算法 1.1 字符串处理 re 正则表达式的标准库. StringIO / cStringIO 以读写文件的方式来操作字符串(有点类似于内存文件). cStringIO 是 C 语言实现的,提供高性能:而 StringIO 是 Python 实现的,提供 Unicode 兼容性. chardet chardet 可以猜测任意一段文本的字符集编码.对于编码类型未知的文本,它会很有用. chardet 既可以作为模块来使用,也可以作为命令行工具来使用. 代码示例 import chardet p

python 各模块

01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支持模块 12 _ _builtin_ _ 模块 121 使用元组或字典中的参数调用函数 1211 Example 1-1 使用 apply 函数 1212 Example 1-2 使用 apply 函数传递关键字参数 1213 Example 1-3 使用 apply 函数调用基类的构造函数 122

python 常用模块(转载)

转载地址:http://codeweblog.com/python-%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97/ adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctypes:用来调用动态链接库DBUtils:数据库连接池django:一个WEB frameworkdocutils:用来写文档的dpkt:数据包的解包和组包My