1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
class ProxSafe(Protocol):
def __init__(self, factory):
self.factory = factory
def connectionMade(self):
self.factory.numProtocols = self.factory.numProtocols+1
self.transport.write(
"Welcome! There are currently %d open connections.\n" %
(self.factory.numProtocols,))
def connectionLost(self, reason):
self.factory.numProtocols = self.factory.numProtocols
def dataReceived(self, data):
self.transport.write(data)
class ProxSafeFactory(Factory):
def __init__(self):
self.factory = Factory
def buildProtocol(self, addr):
self.factory.numProtocols = 0
return ProxSafe(self)
endpoint = TCP4ServerEndpoint(reactor, 4080)
endpoint.listen(ProxSafeFactory())
reactor.run() |
Partager