Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from twisted.internet import protocol, reactor
  2.  
  3.  
  4. class Twist(protocol.Protocol):
  5.     def __init__(self):
  6.         self.a=[]
  7.     # Событие connectionMade срабатывает при соединении
  8.     def connectionMade(self):
  9.  
  10.         print ('connection success!',self.transport.getPeer())
  11.         self.a.append(self.transport.getPeer().port)
  12.         print("a:",self.a)
  13.  
  14.  
  15.     # Событие dataReceived - получение и отправление данных
  16.     def dataReceived(self, data):
  17.         print (data,self.transport.getPeer().port)
  18.         # transport.write - отправка сообщения
  19.         self.transport.write('Hello from server!'.encode())
  20.  
  21.  
  22.     # Событие connectionLost срабатывает при разрыве соединения с клиентом
  23.     def connectionLost(self, reason):
  24.         print ('Connection lost!')
  25.  
  26.  
  27. # Конфигурация поведения протокола описывается в – классе Factory из twisted.internet.protocol.Factory
  28. factory = protocol.Factory()
  29. factory.protocol = Twist
  30. print ('wait...')
  31. reactor.listenTCP(10003, factory)
  32. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement