Pytnon FTP programming-ftplib Module

Source: Internet
Author: User
Tags ftp file ftp login ftp site

Pytnon FTP programming: Python comes with the ftplib module, which is used to upload and download FTP servers. It is very convenient and easy to use.

If we are familiar with the following:CodeWrite, how to download files on the FTP server to the local, how to upload files to the FTP server and batch FTP upload and download

You have mastered Python FTP programming.

 

 1   #  -*-Coding: UTF-8 -*-  
2 # Author: lhj588@live.cn
3 # C_date: 2011.11.17
4 From Ftplib Import FTP
5 Import Socket
6 Import OS
7 # FTP server address
8 Ftp_server = ' 127.0.0.1 '
9 # FTP server port
10 Ftp_port = ' 5433 '
11 # FTP Server login name
12 User_name = ' Lhj588 '
13 # FTP server logon Password
14 Password = ' 888888 '
15
16
17 """
18 Description: simple FTP file download.
19 """
20 Def Upload_ftp ():
21 FTP = FTP ()
22 # Enable debug level 2 to display details
23 FTP. set_debuglevel (2)
24 # Connect to the FTP server
25 FTP. Connect (ftp_server, ftp_port)
26 # Log on to the FTP server. If you log on anonymously, use an empty string instead.
27 FTP. login (user_name, password)
28
29 # Display FTP server welcome information
30 Print FTP. getwelcome ()
31
32 Dir_name =" /Mysoft/tool/ "
33 # Select the directory where the file to be uploaded is stored on the server
34 FTP. CWD (dir_name)
35 # Set the buffer block size
36 Buf_size = 1024
37 # File Name
38 File_name = " Test.jpg "
39 # Open a file locally in write mode
40 F_handler = open (file_name, ' WB ' ). Write ()
41 # Receive files on the FTP server and write them locally
42 FTP. retrbinary ( "Retr Test.jpg " , F_handler, buf_size)
43 # Disable debugging
44 FTP. set_debuglevel (0)
45 # Exit FTP Server
46 FTP. Quit ()
47 """
48 Description: simple FTP file upload.
49 """
50 Def Down_ftp ():
51 FTP = FTP ()
52 # Enable debug level 2 to display details
53 FTP. set_debuglevel (2)
54 # Connect to the FTP server
55 FTP. Connect (ftp_server, ftp_port)
56 # Log on to the FTP server. If you log on anonymously, use an empty string instead.
57 FTP. login (user_name, password)
58 Print FTP. getwelcome ()
59 Dir_name = " /Mysoft/tool/ "
60 FTP. CWD (dir_name)
61 Buf_size = 1024
62 File_name = ' Test.jpg '
63 # Open a file locally in Read mode
64 F_handler = open (file_name, ' RB ' )
65 # Upload files
66 FTP. storbinary ( 'Stor Server_test.jpg ' , F_handler, buf_size)
67 # Disable debugging
68 FTP. set_debuglevel (0)
69 # Close file
70 F_handler.close ()
71 # Exit FTP Server
72 FTP. Quit ()
73
74 """
75 Description: log on to the FTP site: ftp_server, and download the files in the filenametemplist list under the tardirtemp directory.
76 Parameter: ftp_server: ftp site name user_name: FTP login name password: ftp Password
77 Tardirtemp: the absolute directory of the downloaded object filenametemplist: List of full names of the files to be downloaded
78 Returned value: Output: Success: 0 failed: 1
79 """
80 Def Down_ftp_file (ftp_server, user_name, password, tardirtemp, filenametemplist ):
81 Try :
82 FTP = FTP (ftp_server)
83 FTP. login (user_name, password)
84 FTP. CWD (tardirtemp)
85 FTP. set_debuglevel (2)
86 Except Socket. Error as errobj:
87 Print ' FPT error: % s can \'t download directory \'s file % s ' % (Errobj, tardirtemp)
88 Return False
89
90 For Filename In Filenametemplist:
91 Try :
92 # Open a file locally in write mode
93 Fp = open (filename, ' WB ' )
94 # Download files from ftp
95 FTP. retrbinary ( ' RETR ' + Filename, FP. Write, 1024)
96 Except Ioerror as ioerr:
97 Print ' Error: [% s] % s ' % (Ioerr. errno, ioerr. strerror)
98 Return False
99 Except FTP. all_errors as errobj:
100 Print ' FPT error: [% s] % s ' % (Errobj, filename)
101 If OS. Path. isfile (filename ):
102 OS. System ( ' Rm-F ' + Filename)
103 Continue
104 FTP. Close ()
105 Return True
106
107 """
108 Description: test method.
109 """
110 If _ Name __ = " _ Main __ " :
111 Upload_ftp ()
112 Down_ftp ()
113 Down_ftp_file (ftp_server, user_name, password, " /Mysoft/tool/ " ,[ ' 1. jpg ' , ' 2. jpg ' , ' 3. jpg ' ])

 

 

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.