资源简介
用Python自带socket库和tkinter开发的一个简单的聊天室实现。可以创建一个服务器,通过不同的计算机进行聊天。
代码片段和文件信息
import datetime
import json
from enum import Enum
class msgType(Enum):
no_action = 0
login = 1
logout = 2
enter_room = 3
leave_room = 4
send_message = 5
create_room = 6
login_ack = 7
logout_ack = 8
update_room_users = 9
leave_room_ack = 10
create_room_ack = 11
query_room_list = 12
query_room_list_ack = 13
enter_room_ack = 14
update_room_list = 15
tk_login_succeed = 100
tk_login_fail = 101
tk_update_room_list = 102
tk_create_room_ack = 103
tk_enter_room_ack = 104
tk_update_room_user_list = 105
tk_update_msg = 106
class chatRoom:
def __init__(self):
self.__roomname = ‘‘
self.__userList = []
@property
def name(self):
return self.__roomname
@property
def userlist(self):
return self.__userList
@name.setter
def name(self name):
self.__roomname = name
@userlist.setter
def userlist(self userlist):
self.__userList = userlist
def to_json(self):
json_data = {}
json_data[‘name‘] = self.__roomname
json_data[‘userlist‘] = self.__userList
return json.dumps(json_data)
def from_jsonstring(self json_string):
json_data = json.loads(json_string)
self.__roomname = json_data[‘name‘]
self.__userList = json_data[‘userlist‘]
class chatRoomList:
def __init__(self):
self.__roomList = []
@property
def roomlist(self):
return self.__roomList
@roomlist.setter
def roomlist(self roomlist):
self.__roomList = roomlist
def to_json(self):
return json.dumps(self.__roomList)
def from_jsonstring(self json_string):
self.__roomList = json.loads(json_string)
class chatMsg:
__datetime_format = ‘%Y-%m-%d %H:%M:%S‘
def __init__(self):
self.__msg_datetime = datetime.datetime.now()
self.__msg_content = ‘‘
self.__senderName = ‘‘
self.__receiverID = 0
self.__roomname = ‘‘
def to_json(self):
json_data = {}
json_data[‘datetime‘] = self.__msg_datetime.strftime(self.__datetime_format)
json_data[‘content‘] = self.__msg_content
json_data[‘receiver‘] = self.__receiverID
json_data[‘sender‘] = self.__senderName
json_data[‘room‘] = self.__roomname
return json.dumps(json_data)
def from_jsonstring(self json_string):
json_data = json.loads(json_string)
self.__msg_datetime = datetime.datetime.strptime(json_data[‘datetime‘] self.__datetime_format)
self.__msg_content = json_data[‘content‘]
self.__receiverID = json_data[‘receiver‘]
self.__senderName = json_data[‘sender‘]
self.__roomname = json_data
- 上一篇:Flask-login
- 下一篇:python实现的ping工具
相关资源
- python实现的ping工具
- Flask-login
- 矩阵分解在MovieLens上的Python实现
- 全国840个气象站点日照时数转日总太
- python 3.6 socket传输视频
- 基于PYQT编写的人脸识别软件
- 续Python3.x+PyQtChart实现数据可视化界面
- 疯狂Python讲义习题答案.rar
- A*算法解决十五数码问题Python程序、报
- 使用python tkinter写的登陆窗口源码
- caffe_pb2.py
- LUTAI.Tools.IronPythonCheckTool
- 基于python的猜单词游戏开发
- 基于Python实现的Pagerank算法
- Python实现数据库
- 传智播客Python就业班2018.zip
- Python对爬取微博的评论进行jieba分词和
- python爬虫 破解js加密有道词典案列的
- python写的旅游网站源码
- 利用鸢尾花数据集画出P-R曲线 pytho
- pyHook-1.5.1-cp38-cp38-win_amd64.whl
- freesmallgames.zip
- python一加云相册批量爬虫
- 实战python利用线性回归来预测鲍鱼年
- 实战python线性回归
- RSA算法的纯Python实现源码
- 请求分页存储管理Python实现源代码+课
- python爬取100个百度百科页面信息
- python面试题大全
- python视频教程.txt
评论
共有 条评论