zoukankan      html  css  js  c++  java
  • ftplib.FTP 类方法

    在表1 中列出了最常用的方法,这个表并不全面——想查看所有的方法,请参阅模块源代码
    ——但这里列出的方法组成了我们在 Python 中 FTP 客户端编程的“API”。
    也就是说,你不一定要使用其它的方法,因为它们或者是辅助函数,或者是管理函数,或者是
    被 API 调用的。

    方法                                         描述
    login(user='anonymous',         登录到 FTP 服务器,所有的参数都是可选的
    passwd='', acct='')             
    pwd()                                       得到当前工作目录
    cwd(path)                                把当前工作目录设置为 path
    dir([path[,...[,cb]])                     显示 path 目录里的内容,可选的参数 cb 是一个回调函数,它
                                          会被传给 retrlines()方法
    nlst([path[,...])                   与 dir()类似,但返回一个文件名的列表,而不是显示这些文件名
    retrlines(cmd [, cb])             给定 FTP 命令(如“RETR filename”),用于下载文本文件。可选的回调函数 cb 用于处理文件的每一行
    retrbinary(cmd, cb[,            
    bs=8192[, ra]])                    与 retrlines()类似,只是这个指令处理二进制文件。回调函数 cb 用于处理每一块(块大小默认为 8K)下载的数据。
    storlines(cmd, f)                  给定 FTP 命令(如“STOR filename”),以上传文本文件。要给定一个文件对象 f
    storbinary(cmd, f[,               与 storlines()类似,只是这个指令处理二进制文件。要给定一个文件对象 f,上传块大小 bs 默认为 8Kbs=8192])
    bs=8192])
    rename(old, new)                     把远程文件 old 改名为 new
    delete(path)                         删除位于 path 的远程文件
    mkd(directory)                     创建远程目录
    rmd(directory)                     删除远程目录
    quit()                                   关闭连接并退出

    在一般的 FTP 通讯中,要使用到的指令有 login(), cwd(), dir(), pwd(), stor*(), retr*()
    和 quit()。有一些没有列出的 FTP 对象方法也是很有用的。

    ftp的建立既可以先ftp=ftplib.FTP()实例化后再ftp=ftp.connect('user','passwd'),也可以直接实例化链接ftp=ftplib.FTP('user','passwd')请参阅 Python 的文档以得到更多关于
    FTP 对象的信息:
    http://python.org/docs/current/lib/ftp-objects.html


    Example 1展示了如何登陆并获得登陆目录的文件列表. 注意这里的文件列
    表 (列目录操作)格式与服务器有关(一般和主机平台的列目录工具输出格式相
    同, 例如 Unix 下的 ls 和 Windows/DOS 下的 dir )Example 1. 使用 ftplib 模块获得目录列表
    File: ftplib-example-1.py
    import ftplib
    ftp = ftplib.FTP("www.python.org")
    ftp.login("anonymous", "ftplib-example-1")
    print ftp.dir()
    ftp.quit()
    total 34
    drwxrwxr-x
    drwxrwxr-x
    drwxrwxr-x
    lrwxrwxrwx
    welcome.msg
    drwxr-xr-x
    drwxr-sr-x
    drwxrwxr--
    drwxr-xr-x
    ...
    11
    11
    2
    1 root
    root
    root
    root 4127
    4127
    4127
    bin 512
    512
    512
    11 Sep
    Sep
    Sep
    Jun 14
    14
    13
    29 14:18
    14:18
    15:18
    14:34
    3
    3
    2
    3 root
    root
    root
    root wheel
    1400
    4127
    wheel 512
    512
    512
    512 May 19
    Jun 9
    Feb 8
    May 19 1998
    1997
    1998
    1998
    .
    ..
    RCS
    README ->
    bin
    dev
    dup
    etc
    下载文件很简单; 使用合适的 retr 函数即可. 注意当你下载文本文件时, 你
    必须自己加上行结束符. Example 2 中使用了一个 lambda 表达式完成这项
    工作.
    Example 2. 使用 ftplib 模块下载文件
    File: ftplib-example-2.py
    import ftplib
    import sys
    def gettext(ftp, filename, outfile=None):
    # fetch a text file
    if outfile is None:
    outfile = sys.stdout
    # use a lambda to add newlines to the lines read from the server
    ftp.retrlines("RETR " + filename, lambda s, w=outfile.write:
    w(s+" "))
    def getbinary(ftp, filename, outfile=None):# fetch a binary file
    if outfile is None:
    outfile = sys.stdout
    ftp.retrbinary("RETR " + filename, outfile.write)
    ftp = ftplib.FTP("www.python.org")
    ftp.login("anonymous", "ftplib-example-2")
    gettext(ftp, "README")
    getbinary(ftp, "welcome.msg")
    WELCOME to python.org, the Python programming language home site.
    You are number %N of %M allowed users.
    Ni!
    Python Web site: http://www.python.org/
    CONFUSED FTP CLIENT? Try begining your login password with '-' dash.
    This turns off continuation messages that may be confusing your client.
    ...
    最后, Example 3 将文件复制到 FTP 服务器上. 这个脚本使用文件扩展名来
    判断文件是文本文件还是二进制文件.
    Example 3. 使用 ftplib 模块上传文件
    File: ftplib-example-3.py
    import ftplib
    import os
    def upload(ftp, file):
    ext = os.path.splitext(file)[1]
    if ext in (".txt", ".htm", ".html"):
    ftp.storlines("STOR " + file, open(file))
    else:
    ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
    ftp = ftplib.FTP("ftp.fbi.gov")
    ftp.login("mulder", "trustno1")
    upload(ftp, "trixie.zip")
    upload(ftp, "file.txt")
    upload(ftp, "sightings.jpg")

  • 相关阅读:
    File类
    Java运算符
    JAVA语法
    数据库-子查询
    爬取笔趣阁_完本书籍
    爬取动物图片源码
    爬取电影天堂上最新电影的下载链接的源码
    pyinstaller的安装、使用、出错解决办法
    Emmet插件使用方法总结
    Markdown基本语法
  • 原文地址:https://www.cnblogs.com/mycats/p/4216793.html
Copyright © 2011-2022 走看看