user1307957
user1307957

Reputation: 541

Twisted - kick specific client

I want to disconnect specific user in twisted based application. Can somebody explain how I can do it ?

Upvotes: 0

Views: 274

Answers (1)

John
John

Reputation: 13699

from twisted.web import server, resource
from twisted.internet import reactor

bannedIPs = ["127.0.0.1"]

class HelloResource(resource.Resource):
    isLeaf = True
    numberRequests = 0

    def render_GET(self, request):
        clientIP = request.getClientIP()

        if clientIP in bannedIPs:
            return "you are banned"

        self.numberRequests += 1
        request.setHeader("content-type", "text/plain")
        return "I am request #" + str(self.numberRequests) + "\n"

reactor.listenTCP(8080, server.Site(HelloResource()))
reactor.run()

Upvotes: 1

Related Questions