[Twisted-Python] restricting twisted web to a single connection

Andrew Bennetts andrew-twisted at puzzling.org
Tue Jun 17 21:46:04 EDT 2003


On Tue, Jun 17, 2003 at 05:10:55PM -0500, Don Hiatt wrote:
> Hello,
> 
> I'm just getting started with twisted and have a question
> about twisted web. How would I go about limiting the web
> server to accepting a single connection? I know you might
> be asking your self why anyone would want to do such a 
> stupid thing, but I'm controlling some hardware and need
> to restrict access to it to a single user at a time.
> 
> Anyway, I'd appreciate any pointers. :-)

You'll probably have to subclass twisted.web.server.Site to make
buildProtocol return None if a connection is active.  Untested code from the
top of my head:

from twisted.protocols.http import HTTPChannel
from twisted.web.server import Site

class SingleConnectionHTTPChannel(HTTPChannel):
    def connectionLost(self, reason):
        self.factory.connections -= 1
        HTTPChannel.connectionLost(self, reason)

class SingleConnectionSite(Site):
    protocol = SingleConnectionHTTPChannel
    connections = 0
    def buildProtocol(self, addr):
        # Reject concurrent connections
        if self.connections:
            return None
        self.connections += 1

        return Site.buildProtocol(self, addr)

-Andrew.





More information about the Twisted-Python mailing list