Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twisted.internet import protocol, reactor
- class Twist(protocol.Protocol):
- def __init__(self):
- self.a=[]
- # Событие connectionMade срабатывает при соединении
- def connectionMade(self):
- print ('connection success!',self.transport.getPeer())
- self.a.append(self.transport.getPeer().port)
- print("a:",self.a)
- # Событие dataReceived - получение и отправление данных
- def dataReceived(self, data):
- print (data,self.transport.getPeer().port)
- # transport.write - отправка сообщения
- self.transport.write('Hello from server!'.encode())
- # Событие connectionLost срабатывает при разрыве соединения с клиентом
- def connectionLost(self, reason):
- print ('Connection lost!')
- # Конфигурация поведения протокола описывается в – классе Factory из twisted.internet.protocol.Factory
- factory = protocol.Factory()
- factory.protocol = Twist
- print ('wait...')
- reactor.listenTCP(10003, factory)
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement