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)