Example of how to build an FTP server using Python and how to build an ftp server using python

Source: Internet
Author: User
Tags ftp protocol

Example of how to build an FTP server using Python and how to build an ftp server using python

Python version 3.6.2

Use the ftp package: pyftpdlib pip install pyftpdlib to download and install it.

The FTP protocol will be more advantageous in downloading and uploading files when the files are too large, and more convenient for resumable upload and progress monitoring. below is the official document

Basic Method

Import OS from pyftpdlib. authorizers import DummyAuthorizer from pyftpdlib. handlers import FTPHandler from pyftpdlib. servers import FTPServer def main (): # instantiate user authorization management authorizer = DummyAuthorizer () authorizer. add_user ('user', '000000', 'path', perm = 'elradfmwmt') # Add user parameters: username, password, allowed path, permission authorizer. add_anonymous (OS. getcwd () # Anonymous Users are allowed here. If this row cannot be deleted, You Can # instantiate FTPHandler handler = FTPHandler handler. authorizer = authorizer # Set the handler for a client link. banner = "pyftpdlib based ftpd ready. "# handler. masquerade_address = '2017. 25.42.11 '# specify the disguised IP address # handler. passive_ports = range (60000,655 35) # specify the allowed port range address = (ipaddr, 21) # FTP generally uses port server = FTPServer (address, handler) # FTP server instance # set a limit for connections server. max_cons = 256 server. max_cons_per_ip = 5 # enable server. serve_forever () if _ name _ = '_ main _': main ()

After enabling the ftp server, make sure that the firewall has enabled port, and set the Passive ftp check in the internet Options Advanced tab in the browser of the client before logging on to the ftp server.

Log on to the server from Windows:

Use Python to download files from the ftp server

From ftplib import FTP ftp = FTP () ftp. connect ('localhost', 21) # Change localhost to the server IP address ftp. login (user = 'user', passwd = '000000') file = open ('f: // ftpdownload/test.txt ', 'wb') ftp. retrbinary ("RETR test.txt", file. write, 1024) # download an ftp 1024 byte file from the server. set_debuglevel (0) ftp. close ()

FTP server Event Callback Function:

Class MyHandler (FTPHandler): def on_connect (self): # print "% s: % s connected" % (self. remote_ip, self. remote_port) def on_disconnect (self): # Call to close the connection # do something when client disconnects pass def on_login (self, username ): # Call at logon # do something when user login pass def on_logout (self, username): # Call at logout # do something when user logs out pass def on_file_sent (self, file ): # Call do something when a file has been sent pass def on_file_received (self, file) after the file is downloaded ): # Call do something when a file has been encoded ed pass def on_incomplete_file_sent (self, file) after the file is uploaded ): # Call when downloading files # do something when a file is partially sent pass def on_incomplete_file_received (self, file): # Call when uploading files # remove partially uploaded files import OS. remove (file)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.