Guest User

Untitled

a guest
Oct 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. from twisted.internet import protocol, reactor
  2. from twisted.protocols import basic
  3.  
  4. class Presence(basic.LineReceiver):
  5. def connectionMade(self):
  6. self.state = 'needlogin'
  7. def lineReceived(self, line):
  8. cmd, data = line.split(" ", 1)
  9. f = getattr(self, 'handle_%s' % cmd)
  10. f(data)
  11.  
  12. def handle_LOGIN(self, username):
  13. if self.state != 'needlogin':
  14. self.sendLine('ERROR')
  15. return
  16. if self.factory.userOnline(username):
  17. self.sendLine('NO')
  18. else:
  19. self.factory.addUser(username)
  20. self.username = username
  21. self.sendLine('YES')
  22. self.state = 'loggedin'
  23. def handle_ISONLINE(self, username):
  24. if self.state != 'loggedin':
  25. self.sendLine('ERROR')
  26. return
  27. if self.factory.userOnline(username):
  28. self.sendLine('YES')
  29. else:
  30. self.sendLine('NO')
  31.  
  32. def connectionLost(self, reason):
  33. if self.state == 'loggedin':
  34. u = self.username
  35. self.factory.removeUser(u)
  36.  
  37. class PresenceFactory(protocol.ServerFactory):
  38. # used by default buildProtocol:
  39. protocol = Presence
  40.  
  41. def __init__(self):
  42. self.users = {}
  43.  
  44. def addUser(self, user):
  45. self.users[user] = 1
  46.  
  47. def removeUser(self, user):
  48. del self.users[user]
  49.  
  50. def userOnline(self, user):
  51. return self.users.has_key(user)
  52.  
  53.  
  54. f = PresenceFactory()
  55. reactor.listenTCP(9123, f)
  56. reactor.run()
  57.  
  58.  
  59.  
  60. ######### Nash chat fail:
  61.  
  62.  
  63.  
  64. """The most basic chat protocol possible.
  65.  
  66. run me with twistd -y chatserver.py, and then connect with multiple
  67. telnet clients to port 1025
  68. """
  69.  
  70. from twisted.protocols import basic
  71.  
  72.  
  73.  
  74. class MyChat(basic.LineReceiver):
  75.  
  76. people = 0
  77.  
  78. def connectionMade(self):
  79. print "Got new client!"
  80. MyChat.people += 1
  81. self.transport.write("Welcome new client!" + str(MyChat.people) + '\n')
  82. #self.factory.clients.id = int(MyChat.people)
  83. self.factory.usernameId = MyChat.people
  84. self.factory.clients1.append(self)
  85.  
  86. def connectionLost(self, reason):
  87. print "Lost a client!"
  88. MyChat.people -= 1
  89. self.factory.clients1.remove(self)
  90.  
  91. def lineReceived(self, line):
  92. print "received", repr(line)
  93. self.factory.clients1[2].message(line)
  94.  
  95.  
  96.  
  97.  
  98.  
  99. if line == "show":
  100. c.message(str(MyChat.people)) #prints number of clients for the client asking
  101. if line == "myid":
  102. c.message(str(self.factory.usernameId)) #prints number of clients for the client asking
  103.  
  104. def message(self, message):
  105. self.transport.write(message + '\n')
  106.  
  107.  
  108. from twisted.internet import protocol
  109. from twisted.application import service, internet
  110.  
  111. factory = protocol.ServerFactory()
  112. factory.protocol = MyChat
  113. factory.clients1 = []
  114.  
  115. application = service.Application("chatserver")
  116. internet.TCPServer(1025, factory).setServiceParent(application)
Add Comment
Please, Sign In to add comment