先看下服务端的代码
import socket ip_bind = ("127.0.0.1",3000) sk = socket.socket() sk.bind(ip_bind) sk.listen(5) while True: print("server is waiting.....") conn,add = sk.accept() client_data = conn.recv(1024) print(str(client_data,encoding="utf-8")) conn.sendall(bytes("不要不要回答,不要回答,不要回答",encoding="utf-8")) conn.close()
在看下客户端的代码
import socket ip_port = ("127.0.0.1",3000) sk = socket.socket() sk.connect(ip_port) sk.sendall(bytes("请求占领地球",encoding="utf-8")) server_replay = sk.recv(1024) print(str(server_replay,encoding="utf-8")) sk.close()