pyftpdlib的流控机制

2020年7月19日 / 13次阅读 / Last Modified 2020年7月19日
pyftpdlib

流控机制对于一个ftp server来说,是必须的,如果有人上传下载一个超大文件,把数据传输通道占满了,其它业务就连不上了。pyftpdlib有自己的流控机制。

import os

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


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

    dtp_handler = ThrottledDTPHandler
    dtp_handler.read_limit = 30720  # 30 Kb/sec (30 * 1024)
    dtp_handler.write_limit = 30720  # 30 Kb/sec (30 * 1024)

    ftp_handler = FTPHandler
    ftp_handler.authorizer = authorizer
    # have the ftp handler use the alternative dtp handler class
    ftp_handler.dtp_handler = dtp_handler

    server = FTPServer(('', 2121), ftp_handler)
    server.serve_forever()

if __name__ == '__main__':
    main()

通过ThrottledDTPHandler来设置read和write的速率,不过pyftpdlib不是QoS,它是一个应用层的软件,通过一个简单有效的方式打实现流控。

An important feature for an ftpd is limiting the speed for downloads and uploads affecting the data channel. ThrottledDTPHandler.banner can be used to set such limits. The basic idea behind ThrottledDTPHandler is to wrap sending and receiving in a data counter and temporary “sleep” the data channel so that you burst to no more than x Kb/sec average. When it realizes that more than x Kb in a second are being transmitted it temporary blocks the transfer for a certain number of seconds.

pyftpdlib通过临时的sleep的方式,来达到流控的效果,所以我自己的测试,就是感觉传输一卡一卡的,不过不会断,只是慢慢地传。有流控,也减少对其它业务的影响,让多业务并行的效果更好。

-- EOF --

本文链接:https://www.pynote.net/archives/2282

留言区

电子邮件地址不会被公开。 必填项已用*标注


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top