Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7612201
  • 博文数量: 1769
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16352
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1769)

文章存档

2024年(15)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: Python/Ruby

2008-09-11 16:08:27

代码1
 

#!/usr/local/python
#-*- encoding:gb2312 -*-
  
from twisted.internet import protocol
from twisted.protocols import basic
from twisted.python import log
from twisted.internet import reactor
import sys
  
class ConfigServer(basic.LineReceiver):
    def __init__(self):
        pass
  
    def lineReceived(self, line):
        if line == 'quit':
            self.sendLine("Goodbye.")
            self.transport.loseConnection()
        else:
            self.broadcast(line)
   
    def broadcast(self, msg):
        for client in self.factory.clients:
            client.sendLine("%s said: %s" % (self.transport.getPeer().host,msg) );
  
    def connectionMade(self):
        self.factory.clients.append(self)
        print "Connect from %s.." % self.transport.getHost()
        self.sendLine("Welcome...%s" % self.transport.getHost())
  
    def connectionLost(self, reason):
        self.factory.clients.remove(self)
        # self.sendLine("Disconnect...%s" % self.transport.getHost())
        pass
  
class ConfigServerFactory(protocol.ServerFactory):
    protocol = ConfigServer
    clients = []
  
def main():
    log.startLogging(sys.stdout)
    reactor.listenTCP(8080,ConfigServerFactory())
    reactor.run()
   
if __name__ == '__main__':
    main()

 

代码2

 

import socket
import select
  
class ChatServer:
  
  def __init__( self, port ):
    self.port = port;
  
    self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
    self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
    self.srvsock.bind( ("192.168.0.25", port) )
    self.srvsock.listen( 5 )
  
    self.descriptors = [self.srvsock]
    print 'ChatServer started on port %s' % port
  
  
  def run( self ):
  
   while 1:
  
    # Await an event on a readable socket descriptor
    (sread, swrite, sexc) = select.select( self.descriptors, [], [] )
  
    # Iterate through the tagged read descriptors
    for sock in sread:
  
      # Received a connect to the server (listening) socket
      if sock == self.srvsock:
        self.accept_new_connection()
      else:
  
        # Received something on a client socket
        str = sock.recv(100)
  
        # Check to see if the peer socket closed
        if str == '':
          host,port = sock.getpeername()
          str = 'Client left %s:%s\r\n' % (host, port)
          self.broadcast_string( str, sock )
          sock.close
          self.descriptors.remove(sock)
        else:
          host,port = sock.getpeername()
          newstr = '[%s:%s] %s' % (host, port, str)
          self.broadcast_string( newstr, sock )
  
  
  def accept_new_connection( self ):
  
    newsock, (remhost, remport) = self.srvsock.accept()
    self.descriptors.append( newsock )
  
    newsock.send("You're connected to the Python chatserver\r\n")
    str = 'Client joined %s:%s\r\n' % (remhost, remport)
    self.broadcast_string( str, newsock )
  
  def broadcast_string( self, str, omit_sock ):
  
   for sock in self.descriptors:
    if sock != self.srvsock and sock != omit_sock:
      sock.send(str)
  
   print str,
  
myServer = ChatServer( 8080 ).run()

阅读(1881) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~