资源简介
代码片段和文件信息
import socket
import time
class ChatClient:
def __init__(self username port):
self.username = username
self.socket = socket.socket(socket.AF_INET socket.SOCK_STREAM)
self.socket.connect((“127.0.0.1“ port))
def send_msg(self msg):
self.socket.send(“{username}::{msg}“.format(username=self.usernamemsg=msg).encode(“utf-8“))
def recv_msg(self):
data=self.socket.recv(1024)
if data:
print(“\n【机器人小图】“+“ “+time.strftime(‘%Y-%m-%d:%H:%M:%S‘time.localtime(time.time())))
print(data.decode(“utf-8“))
return True
return False
def main(self):
data = self.socket.recv(1024)
print(data.decode(“utf-8“))
msg = input(“请输入消息:“)
self.send_msg(msg)
while True:
if self.recv_msg():
msg=input(“\n我:“)
self.send_msg(msg)
if msg == “exit“:
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1182 2020-04-23 17:46 聊天机器人客户端.py
文件 1996 2020-04-23 17:46 聊天机器人服务端.py
评论
共有 条评论