Python(95)_基于socket的聊天小程序
1、Server端
\'\'\' \'\'\' import socket sk = socket.socket() sk.bind((\'127.0.0.1\',9000)) sk.listen() conn,addr = sk.accept() print(addr) while True: ret = conn.recv(1024).decode(\'utf-8\') if ret == \'bye\': break print(ret) info = input(\'>>>\') conn.send(bytes(info,encoding=\'utf-8\'))
conn.close()
sk.close()
2、Client端
#-*-coding:utf-8-*- \'\'\' client \'\'\' import socket sk = socket.socket() sk.connect((\'127.0.0.1\',9000)) while True: info = input(">>>") sk.send(bytes(info,encoding=\'utf-8\')) ret = sk.recv(1024).decode(\'utf-8\') print(ret) if ret ==\'bye\': sk.send(b\'bye\') break sk.close()
请发表评论