Created
October 28, 2012 13:54
-
-
Save wenshn902/3968659 to your computer and use it in GitHub Desktop.
ftp use pyrhon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: cp936 -*- | |
__author__ = 'wenshn902' | |
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 document | |
delete delete file | |
dir get files list | |
get download file | |
help help | |
mkdir create document | |
put upload file | |
pwd get current path | |
rename rename file name | |
rmdir delete document | |
size get file size | |
''' | |
def main(): | |
try: | |
server=raw_input('enter FTP server info: ') | |
ftp=FTP(server) | |
except (socket.error, socket.gaierror), e: | |
print 'ERROR: cannot reach' | |
return | |
print '*** Connected to host "%s"' % server | |
try: | |
username=raw_input('username:') | |
password=raw_input('password:') | |
ftp.login(username,password) | |
except ftplib.error_perm: | |
print 'ERROR: cannot login' | |
f.quit() | |
return | |
print '*** Logged in as %s' %username | |
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 'type error' | |
except: | |
print 'command error' | |
ftp.quit() | |
return | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment