while True: conn, addr = s.accept() print('Connected with ' + addr[0] + ':' + str(addr[1]))
conn.send(b"Welcome to the server.\n") while True: data = conn.recv(1024) reply = b'OK...' + data if len(data) == 0: continue conn.sendall(reply) conn.close() conn.close()
s.close() ******************** 客户端: ******************** from socket import *
ip_port = ('192.168.1.95', 3600) BUFSIZE = 1024 sk = socket(AF_INET, SOCK_STREAM) sk.connect(ip_port) while True: msg = input('>>:').strip() if len(msg) == 0: continue sk.send(msg.encode('utf-8')) feedback = sk.recv(BUFSIZE) print(feedback.decode('utf-8')) s.close()