【脚本语言系列】关于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,完成上传、下载文件等。
相关文章推荐
- 【脚本语言系列】关于PythonWeb访问urllib,你需要知道的事
- 【脚本语言系列】关于Python调试报错,你需要知道的事
- 【脚本语言系列】关于Python测试工具unittest, 你需要知道的事
- 【脚本语言系列】关于PythonGUI界面PythonWin,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLite,你需要知道的事
- 【脚本语言系列】关于Python图像处理PIL,你需要知道的事
- 【脚本语言系列】关于Python开发环境搭建,你需要知道的事
- 【脚本语言系列】关于Python数值计算Pandas,你需要知道的事
- 【脚本语言系列】关于Python统计分析statsmodel,你需要知道的事
- 【脚本语言系列】关于Python音频处理WMPlayerOCX,你需要知道的事情
- 【脚本语言系列】关于Python测试工具lettuce, 你需要知道的事
- 【脚本语言系列】关于Python机器学习scikits-learn,你需要知道的事
- 【脚本语言系列】关于Python测试工具Pyflakes, 你需要知道的事
- 【脚本语言系列】关于Python邮件服务smtplib,你需要知道的事
- 【脚本语言系列】关于Python游戏工具Pygame,你需要知道的事
- 【脚本语言系列】关于Python测试工具mock, 你需要知道的事
- 【脚本语言系列】关于Python性能分析工具timeit, 你需要知道的事
- 【脚本语言系列】关于Python测试工具docstrings, 你需要知道的事
- 【脚本语言系列】关于Python音频处理DirectSound, 你需要知道的事
- 【脚本语言系列】关于Python邮件服务poplib,你需要知道的事