python There is a built-in ftp client , But to achieve ftp The server also needs to install a third-party library pyftpdlib
pyftpdlib Realize a fully functional asynchronous FTP service , Open source code address :
https://github.com/giampaolo/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:
Official website address :https://pypi.org/project/pip/#downloads; After downloading , decompression
python3 setup.py install
Installation ,
After installation , take pip Add to system environment variables
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 ;
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 ;
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 ;
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
from ftplib import FTP
ftp = FTP()
ftp.connect(host, port)
ftp.login(u,p)
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 ;