多客户连接返回通话

def server(ip, port):
    s = socket.socket()
    s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    s.bind((ip, port))
    s.listen()
    while 1:
        conn , addr = s.accept()
        gevent.spawn(talk, conn, addr)

def talk(conn, addr):
    try:
        while 1:
            res = conn.recv(1024).decode("utf-8")
            print("%s  说: %s" % (addr[0], res))
            conn.send(res.upper().encode("utf-8"))
    except Exception as e:
        print(e)
    finally:
        conn.close()

if __name__ == '__main__':
    server("127.0.0.1", 8989)

 

posted @ 2018-10-30 20:48  python传言  阅读(183)  评论(0编辑  收藏  举报