Limit connection rate in Python Twisted?
Is there any way to limit connection rate in Python Twisted? I need to simulate the slow dataline, with timeouts and optionally 开发者_开发问答data loss and use twisted framework.
Have you considered using Twisted's token buckets?
http://en.wikipedia.org/wiki/Token_bucket http://twistedmatrix.com/documents/current/api/twisted.protocols.htb.html
this post proposes three solutions and discusses the two feasible ones -- the best one is to use iptables
(or other, equally powerful and flexible firewall software, of course) if your OS supports such software (i.e., do the data rate limiting outside of twisted); if your OS has no such power at your disposal, a less preferable but workable solution mentioned there is
1) Create an dictionary {ip1:count1, ip2: count2, .} in the server, and check the counts for each incoming connection. Disconnect with transport.loseConnection() if the threshold for ip:count is exceeded. Reset this dictionary to empty dict {} every minute with reactor.callLater timer.
whose limitation is explained in the post as
approach (1) will do an accept() of the connection and then drop it, giving the host on the other end a syn/ack transaction followed by a closed connection, and then it will probably attempt to reconnect immediately.
精彩评论