spacepaste

 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
34
35
36
37
38
39
40
import socket
import datetime
from twisted.internet import reactor, protocol
from twisted.internet.protocol import Factory, Protocol

class Echo(protocol.Protocol):

    def connectionMade(self):
        print "New client connected"
           
    def dataReceived(self, data):
        print "Msg from the client received"
        if data == "datetime":
           now = datetime.datetime.now()
           self.transport.write("Date and time:")
           self.transport.write(str(now))
        elif data == "clientes":
           self.transport.write("Numbers of clients served: %d " % (self.factory.numClients))
        else:
           self.transport.write("msg received without actions")

class EchoFactory(Factory):

	protocol = Echo

        def __init__(self):
            self.numClients = 0
	
        def clientConnectionMade(self):
 	    self.numClients = self.numClients+1
	    
def main():
    factory = EchoFactory()
    factory.protocol = Echo
    reactor.listenTCP(9000,factory)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()