欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【脚本语言系列】关于PythonWeb访问ftplib,你需要知道的事

程序员文章站 2022-07-14 17:00:13
...

如何使用ftplib

# -*- coding:utf-8 -*-
#
import string
from ftplib import FTP
bufsize = 1024
def Get(filename):
    command = "RETR "+filename
    ftp.retrbinary(command, open(filename, 'wb').write, bufsize)
    print 'Download Successfully'
def Put(filename):
    command = 'STOR '+filename
    filehandler = open(filename, 'rb')
    ftp.storbinary(command, filehandler, bufsize)
    filehandler.close()
    print 'Upload Successfully' 
def PWD():
    print ftp.pwd()
def Size(filename):
    print ftp.size(filename)
def Help():
    print '''
    ============================================
                Simple Python FTP
    ============================================
    cd          Enter Folder
    delete      Delete Folder
    dir         Get Current Folder List
    get         Download Files
    help        help
    mkdir       Create Folder
    put         Upload Files
    pwd         Get Current Document
    rename      rename Folder
    rmdir       remove Folder
    size        Get the size of Files
    '''
server = raw_input('Please input the server of FTP server:')
ftp = FTP(server)
username = raw_input('Please input the user_name:')
password = raw_input('Please input the password:')
ftp.login(username, password)
print ftp.getwelcome()
actions = {"dir":ftp.dir, "pwd":PWD, "cd":ftp.cwd, 'get':Get,
        "put":Put, "help":Help, "rmdir":ftp.rmd,
        "mkdir":ftp.mkd, "delete":ftp.delete,
        "size":Size, "rename":ftp.rename}
while True:
    print 'pyftp>',
    cmds = raw_input()
    cmd = string.split(cmds)
    try:
        if len(cmd) == 1:
            if string.lower(cmd[0]) == "quit":
                break
            else:
                actions[string.lower(cmd[0])]()
        elif len(cmd) == 2:
            actions[string.lower(cmd[0])](cmd[1])
        elif len(cmd) == 3:
            actions[string.lower(cmd[0])](cmd[1], cmd[2])
        else:
            print "Input Error"
    except:
        print "Cmd Error"
ftp.quit()

使用ftp访问上海交大ftp

ftp://ftp.sjtu.edu.cn/

  1. 输入代码
    【脚本语言系列】关于PythonWeb访问ftplib,你需要知道的事
    【脚本语言系列】关于PythonWeb访问ftplib,你需要知道的事

  2. 界面显示
    【脚本语言系列】关于PythonWeb访问ftplib,你需要知道的事
    【脚本语言系列】关于PythonWeb访问ftplib,你需要知道的事

什么是ftplib

Python中的ftplib模块提供了用于访问FTP的函数。
使用ftplib模块可以在Python脚本中访问FTP,完成上传、下载文件等。