All pastes #22175 Raw Edit

echoserver.py

public text v1 · immutable
#22175 ·published 2005-09-05 19:47 UTC
rendered paste body
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor

### Protocol Implementation

# This is just about the simplest possible 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):
        """As soon as any data is received, write it back."""
        info_client = self.transport.getPeer();
        print info_client;
        open("/tmp/teste.txt","w").write(data);
        self.transport.write("OK") # Envia dados para o cliente!
        self.transport.loseConnection() # Fecha a conexão

def main():
    f = Factory()
    f.protocol = Echo
    reactor.listenTCP(8000, f)
    reactor.run()

#Grava em disco dados
#def save_data(data,file_name):
#    path = "/tmp/";
#    open(path,'wb').write(file_name);


if __name__ == '__main__':
    main()