Share an ftp client example using python

Source: Internet
Author: User
Tags ftp file
This article describes how to implement the ftp client in python, including common ftp tasks, upload, download, delete, and rename functions. For more information, see the following code:


#! /Usr/bin/python
# Coding: UTF-8
# Write: JACK
# Info: ftp example
Import ftplib, socket, OS
From time import sleep, ctime

Def LoginFtp (self ):
Ftps = ftplib. FTP ()
Ftps. connect (self. host, self. port)
Ftps. login (self. name, self. passwd)

# No judgment is made on whether the address input is an ip address or a domain name. you can determine whether the input contains <或者实体符号以及';其他可以忽略
Class LoFtp (object ):
'This is ftp class example'
Host = str (raw_input ('host, 127.0.0.1 \ n '))
If host = '': host = '192. 0.0.1'

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

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

Passwd = str (raw_input ('password \ n '))
If passwd = '': passwd =''

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 # set the cache size

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') # open the file and read it.
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) # debug level, 0 no information prompt
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 = 'anonus us', passwd = '', acct ='') log on to the FTP server. all parameters are optional.
# Pwd () to get the current working directory
# Cwd (path) set the current working directory to path
# Dir ([path [,... [, cb]) displays the content in the path directory. the optional parameter cb is a callback function that is passed to the retrlines () method.
# Nlst ([path [,...]) is similar to dir (), but returns a list of file names instead of displaying these file names
# Retrlines (cmd [, cb]) specifies an FTP command (such as "RETR filename") for downloading text files. The optional callback function cb is used to process each row of a file.
# Retrbinary (cmd, cb [, bs = 8192 [, ra]) is similar to retrlines (), but this command is used to process binary files. The callback function cb is used to process data downloaded from each block (the default block size is 8 kB.
# Storlines (cmd, f) specifies the FTP command (for example, "STOR filename") and transmits the text file above. To specify a file object f
# Storbinary (cmd, f [, bs = 8192]) is similar to storlines (), but this command is used to process binary files. To specify a file object f, the size of the uploaded part bs is 8Kbs = 8192] by default.)
# Rename (old, new) rename the remote file old to new
# Delete (path) delete a remote file in path
# Mkd (directory) create a remote directory
# Check for errors for each field to be entered. This function is too small. However, it can be changed according to the actual situation and put in bt as a small tool.
# A little bad, no try

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.