"""Example of rate-limiting your web server.
Caveat emptor: While the transfer rates imposed by this mechanism will
look accurate with wget's rate-meter, don't forget to examine your network
interface's traffic statistics as well. The current implementation tends
to create lots of small packets in some conditions, and each packet carries
with it some bytes of overhead. Check to make sure this overhead is not
costing you more bandwidth than you are saving by limiting the rate!
"""
from twisted.protocols import htb
import shaper
serverFilter = htb.HierarchicalBucketFilter()
serverBucket = htb.Bucket()
serverBucket.maxburst = 20000
serverBucket.rate = 20000
serverFilter.buckets[None] = serverBucket
class WebClientBucket(htb.Bucket):
maxburst = 10000
rate = 1000
webFilter = htb.FilterByHost(serverFilter)
webFilter.bucketFactory = shaper.WebClientBucket
servertype = "web"
if servertype == "web":
from twisted.web import server, static
site = server.Site(static.File("/var/www"))
site.protocol = htb.ShapedProtocolFactory(site.protocol, webFilter)
elif servertype == "chargen":
from twisted.protocols import wire
from twisted.internet import protocol
site = protocol.ServerFactory()
site.protocol = htb.ShapedProtocolFactory(wire.Chargen, webFilter)
from twisted.internet import reactor
reactor.listenTCP(8000, site)
reactor.run()