import socket
import asyncore
import string
from twisted.internet.protocol import Protocol
class Echo(Protocol):
def connectionMade(self):
self.factory.numProtocols = self.factory.numProtocols+1
if self.factory.numProtocols > 100:
self.transport.write("Too many connections, try later")
self.transport.loseConnection()
def connectionLost(self, reason):
self.factory.numProtocols = self.factory.numProtocols-1
def dataReceived(self, data):
self.transport.write(data)
factory = Factory()
factory.protocol = Echo
# 8007 is the port you want to run under. Choose something >1024
endpoint = TCP4ServerEndpoint(reactor, 8007)
endpoint.listen(factory)
reactor.run()
from pants import Connection, engine, Server
class Echo(Connection):
def handle_read(self, data):
self.send(data)
server = Server(Echo).listen() # len(server.channels) will give us the number of connections.
engine.run()
Caveat: Please disregard your personal preference for using Python. I am just looking to find out the technical differences between using a more Python centered engine versus a C-based one as well as any specific pros or cons to using the Twisted engine specifically.