| 1 | from time import time |
|---|
| 2 | from twisted.internet import protocol, reactor |
|---|
| 3 | from twisted.application import service, internet |
|---|
| 4 | |
|---|
| 5 | class NotifyProtocol(protocol.Protocol): |
|---|
| 6 | def dataReceived(self, data): |
|---|
| 7 | now = time() |
|---|
| 8 | change = now - self.factory.lastTime |
|---|
| 9 | if change > self.factory.biggestDelay: |
|---|
| 10 | print 'New slowest:', change |
|---|
| 11 | self.factory.biggestDelay = change |
|---|
| 12 | self.factory.lastTime = now |
|---|
| 13 | self.factory.count += 1 |
|---|
| 14 | if self.factory.count % 1000 == 0: |
|---|
| 15 | print "%s clients have connected." % self.factory.count |
|---|
| 16 | self.transport.write("HTTP/1.x 200 OK\r\n\r\n") |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | class FirstNotifyProtocol(NotifyProtocol): |
|---|
| 20 | def connectionMade(self): |
|---|
| 21 | self.factory.lastTime = time() |
|---|
| 22 | self.factory.protocol = NotifyProtocol |
|---|
| 23 | NotifyProtocol.connectionMade(self) |
|---|
| 24 | |
|---|
| 25 | class NotifyFactory(protocol.ServerFactory): |
|---|
| 26 | protocol = FirstNotifyProtocol |
|---|
| 27 | def __init__(self): |
|---|
| 28 | self.count = 0 |
|---|
| 29 | self.lastTime = time() |
|---|
| 30 | self.biggestDelay = 0 |
|---|
| 31 | |
|---|
| 32 | application = service.Application('NotifyServer') |
|---|
| 33 | internet.TCPServer(2000, NotifyFactory(), interface="0.0.0.0", backlog=50000).setServiceParent(service.IServiceCollection(application)) |
|---|