Python聊天室源码,记录  

从网上找下来的Python聊天室源码, 收集了两个,  两个Python聊天室源码修正了一些错误,试验过来,都能用的, 记录下来。

两个都是可以用telnet登录的, 命令: telnet 服务器ip  8080 ,  windows里就在开始=>运行里输入,linux等系统就在终端输入.

好久没有玩过聊天室了,不过这些Python聊天室源码可以作为Python学习材料, 不错的.

代码一

  1. #!/usr/local/python
  2. #-*- encoding:gb2312 -*-
  3. from twisted.internet import protocol
  4. from twisted.protocols import basic
  5. from twisted.python import log
  6. from twisted.internet import reactor
  7. import sys
  8. class ConfigServer(basic.LineReceiver):
  9. def __init__(self):
  10. pass
  11. def lineReceived(self, line):
  12. if line == ‘quit’:
  13. self.sendLine(“Goodbye.”)
  14. self.transport.loseConnection()
  15. else:
  16. self.broadcast(line)
  17. def broadcast(self, msg):
  18. for client in self.factory.clients:
  19. client.sendLine(“%s said:  %s” % (self.transport.getPeer().host,msg) );
  20. def connectionMade(self):
  21. self.factory.clients.append(self)
  22. print “Connect from %s..” % self.transport.getHost()
  23. self.sendLine(“Welcome…%s” % self.transport.getHost())
  24. def connectionLost(self, reason):
  25. self.factory.clients.remove(self)
  26. # self.sendLine(“Disconnect…%s” % self.transport.getHost())
  27. pass
  28. class ConfigServerFactory(protocol.ServerFactory):
  29. protocol = ConfigServer
  30. clients = []
  31. def main():
  32. log.startLogging(sys.stdout)
  33. reactor.listenTCP(8080,ConfigServerFactory())
  34. reactor.run()
  35. if __name__ == ‘__main__’:
  36. main()

代码2

  1. import socket
  2. import select
  3. class ChatServer:
  4. def __init__( self, port ):
  5. self.port = port;
  6. self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
  7. self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
  8. self.srvsock.bind( (“192.168.0.25”, port) )
  9. self.srvsock.listen( 5 )
  10. self.descriptors = [self.srvsock]
  11. print ‘ChatServer started on port %s’ % port
  12. def run( self ):
  13. while 1:
  14. # Await an event on a readable socket descriptor
  15. (sread, swrite, sexc) = select.select( self.descriptors, [], [] )
  16. # Iterate through the tagged read descriptors
  17. for sock in sread:
  18. # Received a connect to the server (listening) socket
  19. if sock == self.srvsock:
  20. self.accept_new_connection()
  21. else:
  22. # Received something on a client socket
  23. str = sock.recv(100)
  24. # Check to see if the peer socket closed
  25. if str == ”:
  26. host,port = sock.getpeername()
  27. str = ‘Client left %s:%s\r\n’ % (host, port)
  28. self.broadcast_string( str, sock )
  29. sock.close
  30. self.descriptors.remove(sock)
  31. else:
  32. host,port = sock.getpeername()
  33. newstr = ‘[%s:%s] %s’ % (host, port, str)
  34. self.broadcast_string( newstr, sock )
  35. def accept_new_connection( self ):
  36. newsock, (remhost, remport) = self.srvsock.accept()
  37. self.descriptors.append( newsock )
  38. newsock.send(“You’re connected to the Python chatserver\r\n”)
  39. str = ‘Client joined %s:%s\r\n’ % (remhost, remport)
  40. self.broadcast_string( str, newsock )
  41. def broadcast_string( self, str, omit_sock ):
  42. for sock in self.descriptors:
  43. if sock != self.srvsock and sock != omit_sock:
  44. sock.send(str)
  45. print str,
  46. myServer = ChatServer( 8080 ).run()

 

欢迎大佬支持本博客的发展 -- Donate --

本文链接:Python聊天室源码,记录

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:三十岁,谢谢!^^


分享到:          
  1. 没有评论

  1. 没有通告