Python implements FTP client sample sharing _python

Source: Internet
Author: User
Tags anonymous ftp file ftp client

Copy Code code as follows:

#!/usr/bin/python
#coding: Utf-8
#write: JACK
#info: FTP Example
Import ftplib, socket, OS
From time to import sleep, CTime

def loginftp (self):
FTPs = Ftplib. FTP ()
Ftps.connect (Self.host,self.port)
Ftps.login (SELF.NAME,SELF.PASSWD)

#未进行判断地址输入是否为ip或者域名 can be judged whether it contains < or entity symbols as well as '; others can be ignored
Class Loftp (object):
' This is FTP class example '
host = str (raw_input (' host,127.0.0.1\n '))
If host = = ': host = ' 127.0.0.1 '

Port = raw_input (' port,21\n ')
If not (Port.isdigit ()):p ort =21

name = str (raw_input (' name,anonymous\n '))
If name== ': name= ' anonymous '

passwd = str (raw_input (' password\n '))
if passwd = = ':p asswd= '

def zqftp (Self,host,name,passwd,port):
Self.host = Host
Self.name = Name
SELF.PASSWD = passwd
Self.port = Port

def loginftp (self):
Self.ftps = Ftplib. FTP ()
Self.ftps.connect (Self.host,self.port)
Self.ftps.login (SELF.NAME,SELF.PASSWD)
Self.buffer = 2048 #设置缓存大小

def showftp (self):
Self. Loginftp ()
Self.ftps.dir ('/')
dirs = str (raw_input (' Please input dir!\n ')
Print Self.ftps.dir (dirs)


    def upftp (self):
        ' uploads files '
         self. Loginftp ()
        self.ftps.set_debuglevel (2)
         filename = str (raw_input (' Please FILE name!\n '))
         File_open=open (filename, ' RB ') #打开文件 readable
        self.ftps.storbinary (' STOR%s '% os.path.basename (filename), file_open,self.buffer)
        # Uploading files
        self.ftps.set _debuglevel (0)
        file_open.close ()

def delftp (self):
' Delete Files '
Self. Loginftp ()
Filename=str (raw_input (' Please DELETE FILE name!\n '))
Self.ftps.delete (filename)

def removeftp (self):
' Remove File '
Self. Loginftp ()
Self.ftps.set_debuglevel (2) #调试级别, 0 without any information tips
Oldfile=str (raw_input (' Please old FILE name!\n '))
Newfile=str (raw_input (' Please NEW FILE name!\n '))
Self.ftps.rename (Oldfile,newfile)
Self.ftps.set_debuglevel (0)

def downftp (self):
' Download File '
Self. Loginftp ()
Self.ftps.set_debuglevel (2)
Filename=str (raw_input (' Please FILE name!\n '))
File_down = open (filename, ' WB '). Write
Self.ftps.retrbinary (' STOP%s '% os.path.basename (filename), file_down,self.buffer)
Self.ftps.set_debuglevel (0)
File_down.close ()

A = Loftp ()
Print a.showftp ()

While True:
helpn= Str (raw_input (' Whether to continue to view or exit immediately! ( y/n/q) \ n ")
if (helpn== ' Y ') or (helpn== ' y '):
dirs = str (raw_input (' Please input dir!\n ')
A.ftps.dir (dirs)
Elif (helpn== ' Q ') or (helpn== ' Q '):
Exit ()
Else
Break

While True:
print ' upload Please select----1 '
print ' Download Please select----2 '
print ' Modify FTP file name----3 '
num = Int (raw_input (' please input number![ Exit:5]\n '))

If num ==1:
UPF = A.upftp ()
print ' Upfile ok! '
Elif num ==2:
DOF = A.downftp ()
print ' Download file ok! '
Elif num ==3:
ref = A.REMOVEFTP ()
print ' Remove file ok! '
Else
A.ftps.quit ()
print ' bingo! '
Break


#login (user= ' anonymous ', passwd= ', acct= ') Log on to the FTP server, all parameters are optional
#pwd () to get the current working directory
#cwd (path) to set the current working directory to Path
#dir ([path[,...       [, CB]] Displays the contents of the path directory, optional parameter CB is a callback function that is passed to the Retrlines () method
#nlst ([path[,...]) Similar to Dir (), but returns a list of file names rather than displaying them
#retrlines (cmd [, CB]) a given FTP command (such as "RETR filename") for downloading text files. Optional callback function CB is used to process each row of a file
#retrbinary (cmd, cb[,bs=8192[, RA]) is similar to Retrlines (), except that this instruction handles binary files. The callback function CB is used to process each piece of data (the block size defaults to 8K) for downloading.
#storlines (cmd, F) given an FTP command (such as "STOR filename") to upload a text file. To be given a file object F
#storbinary (cmd, f[,bs=8192]) is similar to Storlines (), except that this instruction handles binary files. To be given a file object F, upload block size BS defaults to 8kbs=8192]
#rename (old, new) rename the remote file old to new
#delete (path) deletes a remote file located in path
#mkd (directory) to create a remote directory
#每个需要输入的地方, a troubleshooting check is required. This is just too small a function. But according to the actual situation change, put in BT inside as a small tool can
#有点烂, did not do any try

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.