程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Implementing FTP server and client in Python

編輯:Python

python There is a built-in ftp client , But to achieve ftp The server also needs to install a third-party library pyftpdlib

pyftpdlib

pyftpdlib Realize a fully functional asynchronous FTP service , Open source code address :
https://github.com/giampaolo/pyftpdlib

install pyftpdlib

There are many ways to install , Recommended pip;

pip yes Python Package installer for . Actually ,pip Namely Python Standard library (The Python Standard Library) One of the bags in , It's just that this bag is special , It can be used to manage Python Standard library (The Python Standard Library) Other bags in the .pip Is a command line program . install pip after , A... Will be added to the system pip command , This command can be run from the command prompt .

install pip:

  • install python; This must be installed ;
  • download pip:

    Official website address :https://pypi.org/project/pip/#downloads; After downloading , decompression

  • Open the command line window , Enter into pip Unzipped directory ; Execute code

    python3 setup.py install
    Installation ,
    After installation , take pip Add to system environment variables

  • verification
    Open the command line window , Input pip list perhaps pip3 list

The above needles are for windows System , Other systems can also refer to ;

  • Install the required libraries :
    Open the command line window , Enter and execute the following code , And return

    pip install pyftpdlib

    Wait for the prompt that the third library is successfully installed ;

start-up FTP service

pyftpdlib After successful installation , Enter at the command line

python3 -m pyftpdlib -p 21

You can start a simple ftp service , Not enough to start by default ftp The service has only one anonymous user without password , If you want to add users and permissions , Need to code to implement ;

Code to achieve a user and authority authentication FTP Server side

def ftpServer(): import pyftpdlib # The script is run in windows Upper ftp from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyftpdlib.servers import FTPServer # Instantiate a virtual user  authorizer = DummyAuthorizer() # Add user permissions and paths  authorizer.add_user('user', '123456', "d:/", perm="elradfmw") # Add anonymous users , Just the path  authorizer.add_anonymous("d:/") # initialization ftp Handle  handler = FTPHandler handler.authorizer = authorizer # Add passive port range  handler.passive_ports = range(2000,20033) # monitor ip And port  server = FTPServer(('0.0.0.0', 21), handler) # Start serving  server.serve_forever()

Run the above code , Start a ftp service ;

ftp client

The above has launched a local ftp service , When it comes to testing , have access to windows Own ftp Client connection test ;

Open the command line window , Input
ftp 127.0.0.1
Input user , password
test result

in addition ,python It's also built in ftp Client library ftplib

Use python Medium FTP client

  1. First, import. FTP

from ftplib import FTP

  1. Instantiation FTP object

ftp = FTP()

  1. Connect ftp, Pass in host And port

ftp.connect(host, port)

  1. Log in to ftp, Use login Method , Incoming user password

ftp.login(u,p)

  1. Sign in ftp success , Upload and download files ;

Upload and use storbinary Method :

 buf_size = 1024 file_handler = open(local_file, 'rb') ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size) file_handler.close()

Download the use retrbinary Method

 try: buf_size = 1024 file_handler = open(local_file, 'wb')ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size) file_handler.close() except Exception as err: return

After execution FTP After the operation , You need to close the connection

ftp.quit()

The above has been achieved ftp Daily operation of ; About ftp Other related operations , Please refer to the official documents ;


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved