1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author cc 4 5 import socket; 6 7 ip_port = ("127.0.0.1",9998); 8 9 sk = socket.socket(); 10 sk.connect(ip_port); 11 12 while True: 13 client_input = input(">>:").strip(); 14 if client_input == "q": 15 break; 16 sk.sendall(bytes(client_input,"utf8")); 17 server_replay = sk.recv(1024); 18 print(str(server_replay,"utf8")); 19 20 sk.close();
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author cc 4 5 import socket; 6 7 ip_port = ("127.0.0.1",9998); #绑定需要开放的socket地址以及端口 8 9 sk = socket.socket(); #创建服务端的socket对象socketserver 10 sk.bind(ip_port); 11 sk.listen(5); 12 13 conn,addr = sk.accept(); #accept()函数会返回一个元组,元素1为客户端的socket对象,元素2为客户端的地址(ip地址,端口号) 14 15 while True: 16 client = conn.recv(1024); #客户端输入的数据 17 #strclient = client.decode("utf8") 18 if client == "q": #判断是不是输入的q,是则退出 19 break; 20 print(str(client,"utf8")); #打印出客户端的输入 21 server_response = input("