Home  >  Article  >  Backend Development  >  How to implement FTP server service in Python (Collection)

How to implement FTP server service in Python (Collection)

PHPz
PHPzOriginal
2017-04-12 14:28:502655browse

This article mainly introduces the method of implementing FTP server in python. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look.

Active mode and passive mode of FTP service

Before we start, let’s talk about the active mode and passive mode of FTP. The difference between them may be more clear by using two pictures:

Active mode:

Active mode working process:

1. The client initiates a connection to the server port 21 using a random non-privileged port N, which is a port greater than 1024.

2. The client starts listening to port N+1;

3. The server will actively connect to the client’s N+1 port through port 20.

Advantages of active mode:

The server configuration is simple, which is conducive to server security management. The server only needs to open port 21

Active mode Disadvantages:

If the client has a firewall turned on, or the client is on the intranet (behind a NAT gateway), the connection initiated by the server to the client port may fail

Passive Mode:

Passive mode working process:

1. The client connects to port 21 of the server through a random unprivileged port

2. The server opens an unprivileged port as a passive port and returns it to the client

3. The client actively connects to the server's passive port using the unprivileged port + 1 port

Disadvantages of passive mode:

Server configuration management is slightly complicated, which is not conducive to security. The server needs to open random high-level ports so that clients can connect, so most FTP service software can manually configure the passive port range

Passive Advantages of the mode: There are no requirements for the client network environment

After understanding FTP, start using python to implement FTP services

Preparation work

This The python version used for the first time: python 3.4.3

Install the module pyftpdlib


##

pip3 install pyftpdlib

Create the code file FtpServer.py

Code

Implement simple local verification


from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

#实例化虚拟用户,这是FTP验证首要条件
authorizer = DummyAuthorizer()

#添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限)
authorizer.add_user('user', '12345', '/home/', perm='elradfmw')

#添加匿名用户 只需要路径
authorizer.add_anonymous('/home/huangxm')

#初始化ftp句柄
handler = FTPHandler
handler.authorizer = authorizer

#监听ip 和 端口,因为linux里非root用户无法使用21端口,所以我使用了2121端口
server = FTPServer(('192.168.0.108', 2121), handler)

#开始服务
server.serve_forever()

Start the service

$python FtpServer.py

Test Try:

Enter an incorrect password and try:

The verification failed and you cannot log in.

But this seems to be active mode FTP. How to implement passive mode?

Add passive ports through the following code:

handler.passive_ports = range(2000, 2333)

Full code:


from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

#实例化虚拟用户,这是FTP验证首要条件
authorizer = DummyAuthorizer()

#添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限)
authorizer.add_user('user', '12345', '/home/', perm='elradfmw')

#添加匿名用户 只需要路径
authorizer.add_anonymous('/home/huangxm')

#初始化ftp句柄
handler = FTPHandler
handler.authorizer = authorizer

#添加被动端口范围
handler.passive_ports = range(2000, 2333)

#监听ip 和 端口
server = FTPServer(('192.168.0.108', 2121), handler)

#开始服务
server.serve_forever()

Start the service and you can see the passive port information:


$ python FtpServer.py 
[I 2017-01-11 15:18:37] >>> starting FTP server on 192.168.0.108:2121, pid=46296 <<<
[I 2017-01-11 15:18:37] concurrency model: async
[I 2017-01-11 15:18:37] masquerade (NAT) address: None
[I 2017-01-11 15:18:37] passive ports: 2000->2332

FTP user management:

Through the above practice, the FTP server can already work normally, but what if many FTP users are needed? Should each user write it once?

In fact, we can define a user file user.py


#用户名   密码    权限     目录
# root   12345   elradfmwM  /home
huangxm   12345   elradfmwM  /home

and then traverse the file and add lines that do not start with # to the user_list list

Complete code:


from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer


def get_user(userfile):
  #定义一个用户列表
  user_list = []
  with open(userfile) as f:
    for line in f:
      print(len(line.split()))
      if not line.startswith(&#39;#&#39;) and line:
        if len(line.split()) == 4: 
          user_list.append(line.split())
        else:
          print("user.conf配置错误")
  return user_list

#实例化虚拟用户,这是FTP验证首要条件
authorizer = DummyAuthorizer()

#添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限)
#authorizer.add_user(&#39;user&#39;, &#39;12345&#39;, &#39;/home/&#39;, perm=&#39;elradfmw&#39;)
user_list = get_user(&#39;/home/huangxm/test_py/FtpServer/user.conf&#39;)
for user in user_list:
  name, passwd, permit, homedir = user
  try:
    authorizer.add_user(name, passwd, homedir, perm=permit)
  except Exception as e:
    print(e)

#添加匿名用户 只需要路径
authorizer.add_anonymous(&#39;/home/huangxm&#39;)

#初始化ftp句柄
handler = FTPHandler
handler.authorizer = authorizer

#添加被动端口范围
handler.passive_ports = range(2000, 2333)

#监听ip 和 端口
server = FTPServer((&#39;192.168.0.108&#39;, 2121), handler)

#开始服务
server.serve_forever()

At this point, the FTP service has been completed.

Standardize the code

First create the conf directory to store settings.py and user.py

Directory structure (don’t worry about the cache):

setting.py


ip = &#39;0.0.0.0&#39;

port = &#39;2121&#39;

#上传速度 300kb/s
max_upload = 300 * 1024

#下载速度 300kb/s
max_download = 300 * 1024

#最大连接数
max_cons = 150

#最多IP数
max_per_ip = 10

#被动端口范围,注意被动端口数量要比最大IP数多,否则可能出现无法连接的情况
passive_ports = (2000, 2200)

#是否开启匿名访问 on|off
enable_anonymous = &#39;off&#39;
#匿名用户目录
anonymous_path = &#39;/home/huangxm&#39;

#是否开启日志 on|off
enable_logging = &#39;off&#39;
#日志文件
loging_name = &#39;pyftp.log&#39;

#欢迎信息
welcome_msg = &#39;Welcome to my ftp&#39;

user.py


#用户名   密码    权限     目录
#root   12345   elradfmwM  /home/
huangxm   12345   elradfmwM  /home/
test    12345   elradfmwM  /home/huangxm

FtpServer.py

##

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler, ThrottledDTPHandler
from pyftpdlib.servers import FTPServer
from conf import settings
import logging


def get_user(userfile):
  #定义一个用户列表
  user_list = []
  with open(userfile) as f:
    for line in f:
      if not line.startswith(&#39;#&#39;) and line:
        if len(line.split()) == 4: 
          user_list.append(line.split())
        else:
          print("user.conf配置错误")
  return user_list

def ftp_server():
  #实例化虚拟用户,这是FTP验证首要条件
  authorizer = DummyAuthorizer()
  
  #添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限)
  #authorizer.add_user(&#39;user&#39;, &#39;12345&#39;, &#39;/home/&#39;, perm=&#39;elradfmw&#39;)
  user_list = get_user(&#39;conf/user.py&#39;)
  for user in user_list:
    name, passwd, permit, homedir = user
    try:
      authorizer.add_user(name, passwd, homedir, perm=permit)
    except Exception as e:
      print(e)

  #添加匿名用户 只需要路径
  if settings.enable_anonymous == &#39;on&#39;:
    authorizer.add_anonymous(settings.anonymous_path)
  
  #下载上传速度设置
  dtp_handler = ThrottledDTPHandler
  dtp_handler.read_limit = settings.max_download
  dtp_handler.write_limit = settings.max_upload

  #初始化ftp句柄
  handler = FTPHandler
  handler.authorizer = authorizer

  #日志记录
  if settings.enable_logging == &#39;on&#39;:
    logging.basicConfig(filename=settings.loging_name, level=logging.INFO)

  #欢迎信息
  handler.banner = settings.welcome_msg
  

  #添加被动端口范围
  handler.passive_ports = range(settings.passive_ports[0], settings.passive_ports[1])

  #监听ip 和 端口
  server = FTPServer((settings.ip, settings.port), handler)
  
  #最大连接数
  server.max_cons = settings.max_cons
  server.max_cons_per_ip = settings.max_per_ip
  
  #开始服务
  print(&#39;开始服务&#39;)
  server.serve_forever()

if __name__ == "__main__":
  ftp_server()

Finally, let’s talk about the permission issue

Read permission:

elrWrite permission:
Change file directory
List files
Receive files from server

##aFile uploaddDelete filefRename file mCreate filewWrite permissionMFile Transfer mode (set file permissions via FTP)

M Example:

Go to the server and check the permissions:

You can see that the permissions have been modified .

The above is the detailed content of How to implement FTP server service in Python (Collection). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn