FTP Server by python (pyftpdlib)

0 .背景

最近,需要做一个保存网络摄像机上传的图片,并进行处理的程序,摄像机是通过移动侦测和遮挡侦测发送图片到ftp服务器上. 为了实现上述目标,考虑编写一套ftp server的代码,这样不仅可以保存图片还可以处理不同用户(摄像头)上传的图片,物体检测,人脸识别等可以同步执行.
考虑到目前项目大都在python上,决定采用 pyftpdlib 开发ftp server.

FTP Server by python (pyftpdlib)_第1张图片
c9a2f11637efbb65cecbd64b4e1a8cfa.jpg

1 .安装

这个极为简单:
(python3) pip3 install pyftpdlib
(python2) pip install pyftpdlib

2. Hello World

写一个最简单的ftp server代码.
支持匿名登录,一个用户 'user' 密码 '12345'),拥有全部权限.
匿名用户只有只读权限.
端口2121

import os

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

def main():
    # Instantiate a dummy authorizer for managing 'virtual' users
    authorizer = DummyAuthorizer()

    # Define a new user having full r/w permissions and a read-only
    # anonymous user
    authorizer.add_user('user', '12345', '.', perm='elradfmwMT')
    authorizer.add_anonymous(os.getcwd())

    # Instantiate FTP handler class
    handler = FTPHandler
    handler.authorizer = authorizer

    # Define a customized banner (string returned when client connects)
    handler.banner = "pyftpdlib based ftpd ready."

    # Specify a masquerade address and the range of ports to use for
    # passive connections.  Decomment in case you're behind a NAT.
    #handler.masquerade_address = '151.25.42.11'
    #handler.passive_ports = range(60000, 65535)

    # Instantiate FTP server class and listen on 0.0.0.0:2121
    address = ('', 2121)
    server = FTPServer(address, handler)

    # set a limit for connections
    server.max_cons = 256
    server.max_cons_per_ip = 5

    # start ftp server
    server.serve_forever()

if __name__ == '__main__':
    main()

3. 更完善的代码

具备事件功能,可以监控到上传文件的用户和文件名,可以进一步处理.

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


class MyHandler(FTPHandler):

    def on_connect(self):
        print ("%s:%s connected" % (self.remote_ip, self.remote_port))

    def on_disconnect(self):
        # do something when client disconnects
        pass

    def on_login(self, username):
        # do something when user login
        pass

    def on_logout(self, username):
        # do something when user logs out
        pass

    def on_file_sent(self, file):
        # do something when a file has been sent
        print(self.username, file)
        pass

    def on_file_received(self, file):
        # do something when a file has been received
        print(self.username, file)
        pass

    def on_incomplete_file_sent(self, file):
        # do something when a file is partially sent
        print(self.username, file)
        pass

    def on_incomplete_file_received(self, file):
        # remove partially uploaded files
        import os
        os.remove(file)


def main():
    authorizer = DummyAuthorizer()
    authorizer.add_user('user', '12345', homedir='.', perm='elradfmwMT')
    authorizer.add_anonymous(homedir='.')

    handler = MyHandler
    handler.authorizer = authorizer
    server = FTPServer(('', 2121), handler)
    server.serve_forever()

if __name__ == "__main__":
    main()

4. 性能对比

和主流的vsftp server性能对比


FTP Server by python (pyftpdlib)_第2张图片
Screen Shot 2018-01-12 at 5.55.59 PM.png

5.下一步...

下一步的思路是:
1 . ftp服务监控摄像头上传的图片
2 . 获取用户(那个摄像头)和图片,做物体识别
3 .获取人员或车辆的Box,裁剪图片
4 .根据裁剪图片处理作人员识别或车辆检测
5 .处理结果反馈给对应摄像机的处理设备

你可能感兴趣的:(FTP Server by python (pyftpdlib))