您的位置:首页 > 编程语言 > Python开发

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

2017-06-02 14:52 537 查看

如何使用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/

输入代码





界面显示





什么是ftplib

Python中的ftplib模块提供了用于访问FTP的函数。

使用ftplib模块可以在Python脚本中访问FTP,完成上传、下载文件等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐