Python socket C/S结构的聊天室应用

Linux评论阅读模式

Python socket C/S结构的聊天室应用

服务端:

#!/usr/bin/env python
#coding:utf8

import socket,select
def broadcast_data (sock,message):
    for socket in conn_list:
        if socket != server_socket and socket != sock :
            try :
                socket.send(message)
            except :
                socket.close()
                conn_list.remove(socket)

if __name__ == "__main__":
    conn_list = []
    recv_buffer = 4096
    PORT = 9999

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #端口重用,可选
    server_socket.bind(('', PORT))
    server_socket.listen(99)

    conn_list.append(server_socket)

    print "Chat server started on port " + str(PORT)

    while 1:
        read_sockets,write_sockets,error_sockets = select.select(conn_list,[],[])
        for sock in read_sockets:
            #新建连接
            if sock == server_socket:
                sockfd, addr = server_socket.accept()
                conn_list.append(sockfd)
                print "Client (%s, %s) connected" % addr
                broadcast_data(sockfd, "[%s:%s] entered room\n" % addr)
            #进入聊天室
            else:
                try:
                    data = sock.recv(recv_buffer)
                    if data:
                        broadcast_data(sock, "\r" + '<' + str(sock.getpeername()) + '> ' + data)
                except:
                    broadcast_data(sock, "Client (%s, %s) is offline" % addr)
                    print "Client (%s, %s) is offline" % addr
                    sock.close()
                    conn_list.remove(sock)
                    continue

    server_socket.close()

客户端:

#!/usr/bin/env python
#coding:utf8
import socket,select,string,sys

def prompt() :
    sys.stdout.write('<You> ')
    sys.stdout.flush()

if __name__ == "__main__":

    if(len(sys.argv) < 3) :
        print 'Usage : python chat_client.py hostname port'
        sys.exit()

    HOST = sys.argv[1]
    PORT = int(sys.argv[2])

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)

    try :
        s.connect((HOST, PORT))
    except :
        print 'Unable to connect'
        sys.exit()

    print 'Connected to remote host. Start sending messages'
    prompt()

    while 1:
        socket_list = [sys.stdin, s]

        read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])

        for sock in read_sockets:
            if sock == s:
                data = sock.recv(4096)
                if not data :
                    print '\nDisconnected from chat server'
                    sys.exit()
                else :
                    sys.stdout.write(data)
                    prompt()

            else :
                msg = sys.stdin.readline()
                s.send(msg)
                prompt()

Thu Jul  4 17:24:20 CST 2013

 
  • 本文由 yeho 发表于 2013-07-04
  • 转载请务必保留本文链接:https://linuxeye.com/339.html
脚本

腾讯云COS上传、批量删除工具(Python)

腾讯云对象存储COS是类似于阿里云OSS,相比OSS,COS提供每月免费额度:存储空间50G、外网访问流量10G(内网免费)、免费读请求100万次、写请求10万次。对网站备份来说不错,但是,腾讯云提供...
Python多线程抓取代理服务器 Linux

Python多线程抓取代理服务器

Python作为一门功能强大的脚本语言来说,经常被用来写爬虫程序,下面是Python爬虫多线程抓取代理服务器。 年前是用 //linuxeye.com/340.html 来抓取代理服务器的,谁知道过完...
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证