服务端:
#聊天室测试 import socket import time sk = socket.socket() sk.bind(('192.168.3.12',30031)) sk.listen() conn, addr = sk.accept() #客户端永远开启 while True: if getattr(conn,'_closed'): print("走了一个哥们") conn, addr = sk.accept() print("新来人了!!") msg_byte = conn.recv(1024) msg_str = msg_byte.decode("utf-8") print("客户端:",addr,msg_str) #客户端关闭一个 if msg_str=='bye': conn.close() #time.sleep(5) if getattr(conn, '_closed')==False: send_str = input("我:") conn.send(send_str.encode("utf-8")) sk.close()
客户端:
import socket sk = socket.socket() sk.connect(('192.168.3.12',30031)) #print(k,v) while True: input_str = input("我:") sk.send(input_str.encode('utf-8')) if input_str == 'bye': break data = sk.recv(1024) print("服务器:",data.decode('utf-8')) sk.close()
0则评论给“python-TCP聊天室测试”