欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多客户连接返回通话

程序员文章站 2022-03-15 22:05:06
...
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)