python tcp server 连接多个客户端

python tcp server 供多个客户端连接的例子:

# coding=utf-8
# !/usr/bin/env python


from socket import *
from time import ctime
import threading
import time

HOST = ''
PORT = 1234
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
socks = []  # 放每个客户端的socket


def handle():
    while True:
        for s in socks:
            try:
                data = s.recv(BUFSIZ)  # 到这里程序继续向下执行
            except Exception, e:
                continue
            if not data:
                socks.remove(s)
                continue
            s.send('[%s],%s' % (ctime(), data))


t = threading.Thread(target=handle)  # 子线程
if __name__ == '__main__':
    t.start()
    print 'waiting for connecting...'
    while True:
        clientSock, addr = tcpSerSock.accept()
        print  'connected from:', addr
        clientSock.setblocking(0)
        socks.append(clientSock)


 

你可能感兴趣的:(python)