Skip to content

Instantly share code, notes, and snippets.

@FAROUKix
Forked from wenshn902/ftp2.py
Created January 8, 2016 16:31
Show Gist options
  • Save FAROUKix/e5a1a5fe0ecd8f8ddcbc to your computer and use it in GitHub Desktop.
Save FAROUKix/e5a1a5fe0ecd8f8ddcbc to your computer and use it in GitHub Desktop.
ftp use pyrhon
# -*- 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()
@FAROUKix
Copy link
Author

FAROUKix commented Jan 8, 2016

how i can upload file in ft wit put ?
i use
pyftp ---put /root/Desktop/w1z.php
command error
help me plz :'(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment