Server: import socket sk = socket.socket() sk.bind(('192.168.0.95',8898)) #把地址绑定到套接字 sk.listen() #监听链接 conn,addr = sk.accept() #接受客户端链接 ret = conn.recv(1024) #接收客户端信息 ret = str(ret,encoding='utf-8') print(ret) #打印客户端信息 msg = input('妹子说>>>') msg = bytes(msg,encoding='utf-8') conn.send(msg) #向客户端发送信息 conn.close() #关闭客户端套接字 sk.close() #关闭服务器套接字(可选)
client: import socket sk = socket.socket() # 创建客户套接字 sk.connect(('192.168.0.95',8898)) # 尝试连接服务器 msg = input('你问我>>>>') msg = bytes(msg,encoding='utf-8') sk.send(msg) ret = sk.recv(1024) # 对话(发送/接收) ret = str(ret,encoding='utf-8') print(ret) sk.close() # 关闭客户套接字