Configure FTP server under Linux with Python to access / media directory

On Linux operating system, vsftpd is a common ftp server, but vsftpd cannot set the / media directory as the root directory of ftp. It may also be that I opened it in the wrong way. In short, I didn't operate successfully, so I switched to Python to try to achieve a similar purpose.

Installing Python 3

The Linux system comes with Python version 3.4, which will be supplemented in case of separate installation in the future.

Installing pip3

# wget https://bootstrap.pypa.io/get-pip.py
wget https://bootstrap.pypa.io/pip/3.4/get-pip.py
python3 get-pip.py
pip -V # check version

Install pyftpdlib Library

pip install pyftpdlib

If you don't want to install pip, you can download the build installation package directly from pypi: https://pypi.org/project/pyftpdlib

Fix Chinese garbled errors

The built-in ftp access function of Windows Explorer uses gbk coding, and filesystems Py and handlers Replace 'utf8' in all locations of Py file with 'gbk', otherwise Chinese will be garbled:

import os
import pyftpdlib
root = os.path.dirname(pyftpdlib.__file__)
for file in ['filesystems.py', 'handlers.py']:
    path = os.path.join(root, file)
    with open(path, 'r+') as f:
        s = f.read()
        f.seek(0)
        f.truncate()
        f.write(s.replace("'utf8'", "'gbk'"))

Where filesystems Py, of which 2 are traversed only when Python 2 is used, and the modification does not affect it. 2 are from the function to obtain the folder file list, which should be necessary.

File handlers Py is replaced in 6 places. 2 places are traversed only when Python 2 is used, and 2 places are ftp function to obtain file list, which should be modified as necessary.

There are two other places:

class FTPHandler(AsyncChat):
    ...
    def decode(self, bytes):
        return bytes.decode('utf8', self.unicode_errors)

It has been tested here. If it is not modified, the file cannot be uploaded and downloaded.

class FTPHandler(AsyncChat):
    ...
    def push(self, s):
        asynchat.async_chat.push(self, s.encode('utf8'))

It has been tested here that whether to change it or not will not affect the ftp function, but whether it is necessary to modify it is unknown.

But all the changes have been changed. That's it. If there is a coding problem in the future, you can consider confirming this problem.

Start the FTP server

See Appendix for detailed parameter meanings:

python3 -m pyftpdlib -p 21 -w -d /media/image

Accessing FTP over LAN on Windows

Open Windows explorer and enter in the path:

ftp://192.168.1.xxx/

Configure self start

Open / etc / RC Local, add before the last exit 0:

export LC_ALL="C.UTF-8"
python3 -m pyftpdlib -p 21 -w -d /media/image &

The first line solves the problem of Chinese garbled code displayed by FTP. The solution principle is unknown.

The last & on the second line represents a non blocking script to run.

appendix

pyftpdlib help documentation

Usage: python -m pyftpdlib [options]

Start a stand alone anonymous FTP server.

Options:
  -h, --help
     show this help message and exit

  -i ADDRESS, --interface=ADDRESS
     specify the interface to run on (default all interfaces)

  -p PORT, --port=PORT
     specify port number to run on (default 2121)

  -w, --write
     grants write access for logged in user (default read-only)

  -d FOLDER, --directory=FOLDER
     specify the directory to share (default current directory)

  -n ADDRESS, --nat-address=ADDRESS
     the NAT address to use for passive connections

  -r FROM-TO, --range=FROM-TO
     the range of TCP ports to use for passive connections (e.g. -r 8000-9000)

  -D, --debug
     enable DEBUG logging evel

  -v, --version
     print pyftpdlib version and exit

  -V, --verbose
     activate a more verbose logging

  -u USERNAME, --username=USERNAME
     specify username to login with (anonymous login will be disabled and password required if supplied)

  -P PASSWORD, --password=PASSWORD
     specify a password to login with (username required to be useful)

Reference articles

  1. What are FTP MLSD commands: https://www.cnblogs.com/donot4get/archive/2009/12/08/1619137.html
  2. Pyftpdlib usage: https://blog.csdn.net/xuq09/article/details/84936853
  3. File garbled in Pyftpdlib: https://blog.csdn.net/iteye_13695/article/details/82681602
  4. Set locale to solve the problem of error when entering Chinese in bash: https://www.jianshu.com/p/a7c9ad0ebb01
  5. Linux set boot auto start: https://www.cnblogs.com/ssooking/p/6094740.html

Keywords: Python Linux ftp

Added by nikky_d16 on Sat, 15 Jan 2022 09:44:45 +0200